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

thenable.ts

118 lines3.2 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 if ((thenable as Thenable<T>).status !== 'pending') {
60 // a caller that kept a reference to `resolve`/`reject` can still invoke it
61 // after the promise settled, which the underlying promise ignores. Applying
62 // it here would leave the status disagreeing with the settled value.
63 return
64 }
65​
66 Object.assign(thenable, data)
67​
68 // clear pending props to avoid calling them twice
69 delete (thenable as Partial<PendingThenable<T>>).resolve
70 delete (thenable as Partial<PendingThenable<T>>).reject
71 }
72​
73 thenable.resolve = (value) => {
74 finalize({
75 status: 'fulfilled',
76 value,
77 })
78​
79 resolve(value)
80 }
81 thenable.reject = (reason) => {
82 finalize({
83 status: 'rejected',
84 reason,
85 })
86​
87 reject(reason)
88 }
89​
90 return thenable
92​
93/**
94 * This function takes a Promise-like input and detects whether the data
95 * is synchronously available or not.
96 *
97 * It does not inspect .status, .value or .reason properties of the promise,
98 * as those are not always available, and the .status of React's promises
99 * should not be considered part of the public API.
100 */
101export function tryResolveSync(promise: Promise<unknown> | Thenable<unknown>) {
102 let data: unknown
103​
104 promise
105 .then((result) => {
106 data = result
107 return result
108 }, noop)
109 // .catch can be unavailable on certain kinds of thenable's
110 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
111 ?.catch(noop)
112​
113 if (data !== undefined) {
114 return { data }
115 }
116​
117 return undefined