Home > Blockchain >  cannot call promise inside of function: "has no call signatures"
cannot call promise inside of function: "has no call signatures"

Time:08-05

I have this class:

export class ExponentialBackoffUtils {
    public static retry(promise: Promise<any>, maxRetries: number, onRetry?: Function) {
        function waitFor(milliseconds: number) {
            return new Promise((resolve) => setTimeout(resolve, milliseconds));
        }
        async function retryWithBackoff(retries: number) {
            try {
                if (retries > 0) {
                    const timeToWait = 2 ** retries * 1000;
                    await waitFor(timeToWait);
                }
                console.log('Retries: ', retries);
                if (retries < 3) {
                    throw new Error();
                }
                return await promise();
            } catch (e) {
                if (retries < maxRetries) {
                    if (onRetry) {
                        onRetry();
                    }
                    return retryWithBackoff(retries   1);
                } else {
                    throw e;
                }
            }
        }

        return retryWithBackoff(0);
    }
}

Typescript is yelling at me saying I can't call the promise that I pass in......why? It says that promise has no call signatures.

enter image description here

CodePudding user response:

A Promise is not a function, so you cannot call it with the promise() syntax.

It should be sufficient for you to simply await promise without the brackets.

  • Related