runWithTimeout (runtime/timeout.ts)

fastgpt · packages/dal/redis/runtime/timeout.ts · typescript · Apache-2.0
database

Runs a promise with a timeout, rejecting if exceeded.

// Source: https://github.com/labring/fastgpt | License: Apache-2.0 | Copyright 2023 Sealos
// Provided via justclaw.it.com; keep this notice with the code.
/** 在指定 deadline 内等待 operation;超时只终止等待,不取消底层 Redis 操作。 */
export const runWithTimeout = <T>({
  operation,
  timeoutMs,
  timeoutMessage
}: {
  operation: Promise<T>;
  timeoutMs: number;
  timeoutMessage: string;
}): Promise<T> => {
  return new Promise<T>((resolve, reject) => {
    const timeout = setTimeout(() => reject(new Error(timeoutMessage)), timeoutMs);

    operation.then(resolve, reject).finally(() => clearTimeout(timeout));
  });
};

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key