- Notifications
You must be signed in to change notification settings - Fork5
A Promise+ compatible abstraction that defers resolving/rejecting promises to another closure.
open-draft/deferred-promise
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
TheDeferredPromise class is a Promise-compatible abstraction that defers resolving/rejecting promises to another closure. This class is primarily useful when one part of your system establishes as promise but another part of your system fulfills it.
This class is conceptually inspired by the
createDeferredPromise()internal utility in Node.js. Unlike the Node.js implementation, however,DeferredProimseextends a nativePromise, allowing the consumer to handle deferred promises like regular promises (no.promiseinstance nesting).
npm install @open-draft/deferred-promise
Creates a Promise executor function that delegates its resolution to the current scope.
import{createDeferredExecutor}from'@open-draft/deferred-promise'constexecutor=createDeferredExecutor()constpromise=newPromise(executor)executor.resolve('hello')// executor.reject(new Error('Reason'))
Deferred executor allows you to control any promise remotely and doesn't affect the Promise instance in any way. Similar to theDeferredPromise instance, the deferred executor exposes additional promise properties likestate,rejectionReason,resolve, andreject. In fact, theDeferredPromise class is implemented on top of the deferred executor.
constexecutor=createDeferredExecutor()constpromise=newPromise(executor)executor.reject('reason')nextTick(()=>{console.log(executor.rejectionReason)// "reason"})
<"pending" | "fulfilled" | "rejected">Default:"pending"
constexecutor=createDeferredExecutor()constpromise=newPromise(executor)console.log(executor.state)// "pending"
Callingresolve() andreject() methods of the executor transitions the state to "fulfilled" and "rejected" respectively.
Resolves the promise with a given value.
constexecutor=createDeferredExecutor()constpromise=newPromise(executor)console.log(executor.state)// "pending"executor.resolve()// The promise state is still "pending"// because promises are settled in the next microtask.console.log(executor.state)// "pending"nextTick(()=>{// In the next microtask, the promise's state is resolved.console.log(executor.state)// "fulfilled"})
Rejects the promise with a given reason.
constexecutor=createDeferredExecutor()constpromise=newPromise(executor)executor.reject(newError('Failed to fetch'))nextTick(()=>{console.log(executor.state)// "rejected"console.log(executor.rejectionReason)// Error("Failed to fetch")})
You can access the rejection reason of the promise at any time by therejectionReason property of the deferred executor.
Returns the reason of the promise rejection. If no reason has been provided to thereject() call,undefined is returned instead.
constexecutor=createDeferredExecutor()constpromise=newPromise(executor)executor.reject(newError('Internal Server Error'))nextTick(()=>{console.log(executor.rejectionReason)// Error("Internal Server Error")})
Creates a new instance of a deferred promise.
import{DeferredPromise}from'@open-draft/deferred-promise'constpromise=newDeferredPromise()
A deferred promise is a Promise-compatible class that constructs a regular Promise instance under the hood, controlling it via thedeferred executor.
A deferred promise is fully compatible with the regular Promise, both type- and runtime-wise, e.g. a deferred promise can be chained and awaited normally.
constpromise=newDeferredPromise().then((value)=>value.toUpperCase()).then((value)=>value.substring(0,2)).catch((error)=>console.error(error))awaitpromise
Unlike the regular Promise, however, a deferred promise doesn't accept theexecutor function as the constructor argument. Instead, the resolution of the deferred promise is deferred to the current scope (thus the name).
functiongetPort(){// Notice that you don't provide any executor function// when constructing a deferred promise.constportPromise=newDeferredPromise()port.on('open',(port)=>{// Resolve the deferred promise whenever necessary.portPromise.resolve(port)})// Return the deferred promise immediately.returnportPromise}
Use theresolve() andreject() methods of the deferred promise instance to resolve and reject that promise respectively.
SeeDeferredExecutor.rejectionReason
- Jonas Kuske for the phenomenal work around improving Promise-compliance.
About
A Promise+ compatible abstraction that defers resolving/rejecting promises to another closure.
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.
Contributors4
Uh oh!
There was an error while loading.Please reload this page.