p-state-defer
v1.0.1
Published
A deferred promise with state
Downloads
9
Readme
p-state-defer
A deferred promise with state
Install
npm i p-state-defer
// esm
import Deferred from 'p-state-defer'
// or cjs
const {Deferred} = require('p-state-defer')
Usage
Basic
function loadImage(url){
// Unlike Promise, No Indent!
const defer = new Deferred();
const img = new Image()
img.src = url
img.onload = _ => defer.resolve(img)
img.onerror = _ => defer.reject(new Error('Failed to load image.'))
return defer.promise;
}
State
const defer = new Deferred();
console.log( defer.isCompleted ) // false
console.log( defer.state ) // "pending"
defer.resolve('hello')
console.log( defer.isCompleted ) // => true
console.log( defer.state ) // => "resolved"
API
Deferred
Deferred Constructor.
const defer = new Deferred();
Properties
promise : Promise
Returns Promise.
defer.promise.then(...)
state : 'pending' | 'resolved' | 'rejected'
Returns current state.
isCompleted : boolean
Returns whether completed or not.
Methods
resolve(value: T)
Resolve promise.
reject(reason: any)
Reject promise.
License
MIT