All files / toolbox / promises.ts

100.00% Branches 0/0
100.00% Lines 8/8
1
2
3
4
5
6
7
8
9
 
x1
x5
x5
x20
x5
x5
x5
x5






/** Returns the current state of a promise. */
export async function state(promise: Promise<unknown>): Promise<"pending" | "fulfilled" | "rejected"> {
  const check = new Promise<void>((resolve) => setTimeout(resolve, 0))
  try {
    return await Promise.race([promise.then((_) => "fulfilled" as const).catch((_) => "rejected" as const), check.then((_) => "pending" as const)])
  } finally {
    await check
  }
}