Ming
Ming

Reputation: 1612

typescript retry logic with timeout ms

hello I'm trying to implement, a retry logic in my connection with rabbitmq, with this:

retry logic:

export const retry: <T>(
  operation: () => Promise<T>,
  maxRetries: number,
  waitTimeMSeconds: number
) => Promise<T> = <T>(
  operation: () => Promise<T>,
  maxRetries: number,
  waitTimeMSeconds: number
) =>
  new Promise<T>((resolve) => {
    operation()
      .then(resolve)
      .catch((error) =>
        maxRetries > 0
          ? setTimeout(() => {
              retry(operation, maxRetries, waitTimeMSeconds).then(resolve);
            }, waitTimeMSeconds)
          : Promise.reject(error)
      );
  });

my operation function:

   this.connection = await retry<Connection>(
      connect(this.rabbitUrl),
      10,
      1000
    );

but

Argument of type 'Bluebird' is not assignable to parameter of type '() => Promise'. Type 'Bluebird' provides no match for the signature '(): Promise'.ts(2345)

but I am getting errors of types in my connect

Upvotes: 0

Views: 1166

Answers (1)

Nicholas Tower
Nicholas Tower

Reputation: 85152

this.connection = await retry<Connection>(
  connect(this.rabbitUrl),
  10,
  1000
);

You need to pass a function in as the first parameter, but currently you're calling the function yourself and then passing its return value in. That return value is apparently a bluebird promise.

Instead, create a function and pass that in:

this.connection = await retry<Connection>(
  () => connect(this.rabbitUrl),
  10,
  1000
);

Upvotes: 1

Related Questions