Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

thenable.ts

111 lines2.9 KB

1/**
2 * Thenable types which matches React's types for promises
3 *
4 * React seemingly uses `.status`, `.value` and `.reason` properties on a promises to optimistically unwrap data from promises
5 *
6 * @see https://github.com/facebook/react/blob/main/packages/shared/ReactTypes.js#L112-L138
7 * @see https://github.com/facebook/react/blob/4f604941569d2e8947ce1460a0b2997e835f37b9/packages/react-debug-tools/src/ReactDebugHooks.js#L224-L227
8 */
9​
10import { noop } from './utils'
11​
12interface Fulfilled<T> {
13 status: 'fulfilled'
14 value: T
16interface Rejected {
17 status: 'rejected'
18 reason: unknown
20interface Pending<T> {
21 status: 'pending'
22​
23 /**
24 * Resolve the promise with a value.
25 * Will remove the `resolve` and `reject` properties from the promise.
26 */
27 resolve: (value: T) => void
28 /**
29 * Reject the promise with a reason.
30 * Will remove the `resolve` and `reject` properties from the promise.
31 */
32 reject: (reason: unknown) => void
34​
35export type FulfilledThenable<T> = Promise<T> & Fulfilled<T>
36export type RejectedThenable<T> = Promise<T> & Rejected
37export type PendingThenable<T> = Promise<T> & Pending<T>
38​
39export type Thenable<T> =
40 | FulfilledThenable<T>
41 | RejectedThenable<T>
42 | PendingThenable<T>
43​
44export 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 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
85​
86/**
87 * This function takes a Promise-like input and detects whether the data
88 * is synchronously available or not.
89 *
90 * It does not inspect .status, .value or .reason properties of the promise,
91 * as those are not always available, and the .status of React's promises
92 * should not be considered part of the public API.
93 */
94export function tryResolveSync(promise: Promise<unknown> | Thenable<unknown>) {
95 let data: unknown
96​
97 promise
98 .then((result) => {
99 data = result
100 return result
101 }, noop)
102 // .catch can be unavailable on certain kinds of thenable's
103 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
104 ?.catch(noop)
105​
106 if (data !== undefined) {
107 return { data }
108 }
109​
110 return undefined