Skip to content

promise

createQueryable

Signature

js
promise.createQueryable (promiseOrExecutor)

Description

Wraps a Promise or an executor function to make its state synchronously inspectable. By default, JavaScript promises expose no way to know their current state — this function adds isPending(), isFulfilled(), isRejected(), and getStatus() methods to the returned promise.

If the promise passed in is already queryable, it is returned as-is.

Parameters

NameTypeRequiredDescription
promiseOrExecutorPromise | functionyesA promise or an executor function (resolve, reject) => void

Returns

TypeDescription
PromiseThe original promise, augmented with state inspection methods
MethodReturnsDescription
isPending()booleantrue if the promise has not yet settled
isFulfilled()booleantrue if the promise resolved successfully
isRejected()booleantrue if the promise was rejected
getStatus()stringOne of 'pending', 'fulfilled', 'rejected'

Throws

Throws a TypeError if promiseOrExecutor is neither a Promise nor a function.

Examples

js
const p = promise.createQueryable(fetch('/api/data'))

p.isPending()   // true
p.isFulfilled() // false
p.isRejected()  // false
p.getStatus()   // 'pending'

await p

p.isPending()   // false
p.isFulfilled() // true
p.isRejected()  // false
p.getStatus()   // 'fulfilled'
js
// Also accepts an executor function
const p = promise.createQueryable((resolve, reject) => {
  setTimeout(() => resolve(42), 1000)
})

p.isPending() // true
p.getStatus() // 'pending'
js
// Returns the same instance if already queryable
const p1 = promise.createQueryable(Promise.resolve(42))
const p2 = promise.createQueryable(p1)
p1 === p2 // true