()
| 42 | | PendingThenable<T> |
| 43 | |
| 44 | export function pendingThenable<T>(): PendingThenable<T> { |
| 45 | let resolve: Pending<T>['resolve'] |
| 46 | let reject: Pending<T>['reject'] |
| 47 | // this could use `Promise.withResolvers()` in the future |
| 48 | const thenable = new Promise((_resolve, _reject) => { |
| 49 | resolve = _resolve |
| 50 | reject = _reject |
| 51 | }) as PendingThenable<T> |
| 52 | |
| 53 | thenable.status = 'pending' |
| 54 | thenable.catch(() => { |
| 55 | // prevent unhandled rejection errors |
| 56 | }) |
| 57 | |
| 58 | function finalize(data: Fulfilled<T> | Rejected) { |
| 59 | Object.assign(thenable, data) |
| 60 | |
| 61 | // clear pending props props to avoid calling them twice |
| 62 | delete (thenable as Partial<PendingThenable<T>>).resolve |
| 63 | delete (thenable as Partial<PendingThenable<T>>).reject |
| 64 | } |
| 65 | |
| 66 | thenable.resolve = (value) => { |
| 67 | finalize({ |
| 68 | status: 'fulfilled', |
| 69 | value, |
| 70 | }) |
| 71 | |
| 72 | resolve(value) |
| 73 | } |
| 74 | thenable.reject = (reason) => { |
| 75 | finalize({ |
| 76 | status: 'rejected', |
| 77 | reason, |
| 78 | }) |
| 79 | |
| 80 | reject(reason) |
| 81 | } |
| 82 | |
| 83 | return thenable |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * This function takes a Promise-like input and detects whether the data |
no test coverage detected