@sanity/comlink
Version:
A library for one-to-many cross-origin communication between Window contexts, built on the postMessage API.
21 lines (17 loc) • 542 B
text/typescript
// Returns Promise.withResolvers or polyfill if unavailable
export function createPromiseWithResolvers<T = unknown>(): {
promise: Promise<T>
resolve: (value: T | PromiseLike<T>) => void
reject: (reason?: any) => void
} {
if (typeof Promise.withResolvers === 'function') {
return Promise.withResolvers<T>()
}
let resolve!: (value: T | PromiseLike<T>) => void
let reject!: (reason?: any) => void
const promise = new Promise<T>((res, rej) => {
resolve = res
reject = rej
})
return {promise, resolve, reject}
}