A TypeScript implementation of the deferred pattern, providing explicit control over promise resolution and rejection.
pnpm add @escapace/deferredimport { Deferred } from '@escapace/deferred'
// Create a deferred
const deferred = new Deferred<string>()
// Access the promise
const promise = deferred.promise
// Resolve the deferred
deferred.resolve('success')
// Or reject it
// deferred.reject('error')
// Check state
console.log(deferred.isFulfilled()) // true
console.log(deferred.isResolved()) // trueCreates a new deferred instance.
Properties:
promise: Promise<T>- The underlying promise
Methods:
Resolves the deferred with the given value.
Rejects the deferred with the given reason.
Returns true if the promise is still pending.
Returns true if the promise was resolved successfully.
Returns true if the promise was rejected.
Returns true if the deferred has been resolved or rejected.
Error Handling:
- Attempting to resolve or reject a deferred twice throws an error: "Deferred cannot be resolved twice"
MPL-2.0