ProxyPromise
A ProxyPromise
is a Promise
that can be manually settled and inspected. It
decouples the creation of the ProxyPromise
object from the conditions that will
resolve or reject it.
For example, a function dispatching tasks to background workers can create a
ProxyPromise
and return it immediately. A worker can later grab the ProxyPromise
and settle it, while the original caller is waiting.
npm install proxy-promise
The package includes Typescript definitions.
Usage
The ProxyPromise
constructor takes no parameters:
const promise =
You can invoke .resolve(value)
or .reject(error)
to settle the ProxyPromise
at any later time:
const p1 = p1 const p2 = p2
Like a regular Promise
, a ProxyPromise
will ignore any calls to resolve
or reject
after the first one.
Calling then
or catch
on the ProxyPromise
will return a regular Promise
.
// now a regular `Promise` // now also a regular `Promise`
Only the original ProxyPromise
object has the resolve
and reject
methods.
You can prevent other actors from settling the promise by calling then
,
giving them a regular Promise
.
Inspection
You can inspect a ProxyPromise
by looking at its state
property. The state
is set to 'pending'
at creation, then to 'resolved'
or 'rejected'
.
-
When
state
is'resolved'
, theProxyPromise
will have avalue
property. -
When
state
is'rejected'
, theProxyPromise
will have anerror
property.
Extras
The resolve
and reject
methods of a ProxyPromise
are already bound, so they
can be passed as arguments without using .bind()
.
The ProxyPromise
also comes with a callback
property, a classic-style function(error, value)
that will invoke reject(error)
or resolve(value)
, depending on whether error == null
.
const promise = promise fs