MCPcopy Index your code
hub / github.com/mobxjs/mobx / whenPromise

Function whenPromise

packages/mobx/src/api/when.ts:72–98  ·  view source on GitHub ↗
(
    predicate: () => boolean,
    opts?: IWhenOptions
)

Source from the content-addressed store, hash-verified

70}
71
72function whenPromise(
73 predicate: () => boolean,
74 opts?: IWhenOptions
75): Promise<void> & { cancel(): void } {
76 if (__DEV__ && opts && opts.onError) {
77 return die(`the options 'onError' and 'promise' cannot be combined`)
78 }
79 if (opts?.signal?.aborted) {
80 return Object.assign(Promise.reject(new Error("WHEN_ABORTED")), { cancel: () => null })
81 }
82 let cancel
83 let abort
84 const res = new Promise((resolve, reject) => {
85 let disposer = _when(predicate, resolve as Lambda, { ...opts, onError: reject })
86 cancel = () => {
87 disposer()
88 reject(new Error("WHEN_CANCELLED"))
89 }
90 abort = () => {
91 disposer()
92 reject(new Error("WHEN_ABORTED"))
93 }
94 opts?.signal?.addEventListener?.("abort", abort)
95 }).finally(() => opts?.signal?.removeEventListener?.("abort", abort))
96 ;(res as any).cancel = cancel
97 return res as any
98}

Callers 1

whenFunction · 0.85

Calls 2

dieFunction · 0.85
_whenFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…