MCPcopy Create free account
hub / github.com/scriptscat/scriptcat / withTimeoutNotify

Function withTimeoutNotify

src/pkg/utils/concurrency-control.ts:37–62  ·  view source on GitHub ↗
(
  promise: Promise<T>,
  time: number,
  fn: (res: TWithTimeoutNotifyResult<T>) => any
)

Source from the content-addressed store, hash-verified

35 err: undefined | Error;
36};
37export const withTimeoutNotify = <T>(
38 promise: Promise<T>,
39 time: number,
40 fn: (res: TWithTimeoutNotifyResult<T>) => any
41) => {
42 const res: TWithTimeoutNotifyResult<T> = { timeouted: false, result: undefined, settled: false, err: undefined };
43 const cid = setTimeout(() => {
44 res.timeouted = true;
45 fn(res);
46 }, time);
47 return promise
48 .then((result: T) => {
49 clearTimeout(cid);
50 res.result = result;
51 res.settled = true;
52 })
53 .catch((e) => {
54 clearTimeout(cid);
55 res.err = e;
56 res.settled = true;
57 })
58 .then(() => {
59 fn(res);
60 return res;
61 });
62};

Callers 2

Calls

no outgoing calls

Tested by

no test coverage detected