This repo is DEPRECATED in favor of promise-controller!
A Promise wrapping library for advanced control of promise lifecycle. Allows to split business logic from promise manipulation:
- automatically store
resolve
/reject
functions for future call - return existing promise while async operation is still pending
- auto-reject after configured timeout
npm install controlled-promise --save
const ControlledPromise = require('controlled-promise');
// Create controlled promise
const controlledPromise = new ControlledPromise();
// Calls asynchronous function and returns promise.
// The `resolve / reject` functions can be called later as `controlledPromise.resolve() / controlledPromise.reject()`.
const promise = controlledPromise.call(() => someAsyncFn());
// Resolve promise later via cp.resolve()
controlledPromise.resolve();
// OR reject promise later via cp.reject()
controlledPromise.reject(error);
Controlled Promise.
Kind: global class
- ControlledPromise
- new ControlledPromise()
- .promise ⇒
Promise
- .value ⇒
*
- .isPending ⇒
Boolean
- .isFulfilled ⇒
Boolean
- .isRejected ⇒
Boolean
- .isSettled ⇒
Boolean
- .isCalled ⇒
Boolean
- .call(fn) ⇒
Promise
- .resolve([value])
- .reject([value])
- .reset()
- .timeout(ms, [reason])
Creates controlled promise. In contrast to original Promise, it does not immediately call any function.
Instead it has .call() method for that and resolve / reject
methods for
resolving promise.
Returns promise itself.
Kind: instance property of ControlledPromise
Returns value with that promise was fulfilled (resolved or rejected).
Kind: instance property of ControlledPromise
Returns true if promise is pending.
Kind: instance property of ControlledPromise
Returns true if promise is fulfilled.
Kind: instance property of ControlledPromise
Returns true if promise rejected.
Kind: instance property of ControlledPromise
Returns true if promise fulfilled or rejected.
Kind: instance property of ControlledPromise
Returns true if promise already called via .call()
method.
Kind: instance property of ControlledPromise
This method executes fn
and returns promise. While promise is pending all subsequent calls of .call(fn)
will return the same promise. To fulfill that promise you can use .resolve() / .reject()
methods.
Kind: instance method of ControlledPromise
Param | Type |
---|---|
fn | function |
Resolves pending promise with specified value
.
Kind: instance method of ControlledPromise
Param | Type |
---|---|
[value] | * |
Rejects pending promise with specified value
.
Kind: instance method of ControlledPromise
Param | Type |
---|---|
[value] | * |
Resets to initial state.
Kind: instance method of ControlledPromise
Sets timeout to reject promise automatically.
Kind: instance method of ControlledPromise
Param | Type | Description |
---|---|---|
ms | Number |
delay in ms after that promise will be rejected automatically |
[reason] | String | Error | function |
rejection value. If it is string or error - promise will be rejected with that error. If it is function - this function will be called after delay where you can manually resolve or reject promise via .resolve() / .reject() methods. |
MIT @ Vitaliy Potapov