Skip to content

Instantly share code, notes, and snippets.

@tec27
Created July 9, 2016 02:15
Show Gist options
  • Save tec27/e2f8776dcf47d31057ca1b229ff7d9f7 to your computer and use it in GitHub Desktop.
Save tec27/e2f8776dcf47d31057ca1b229ff7d9f7 to your computer and use it in GitHub Desktop.
An extension of ES6 Promises that allows for easier deferred resolution/rejection
class Deferred extends Promise {
constructor(executor) {
super(executor)
// These will be overwritten by the creator function
this._resolve = null
this._reject = null
}
resolve(value) {
this._resolve(value)
}
reject(err) {
this._reject(err)
}
}
export default function createDeferred() {
let _resolve, _reject
const deferred = new Deferred((resolve, reject) => {
// This executes *immediately* (before the stuff later on in the outer function)
_resolve = resolve
_reject = reject
})
deferred._resolve = _resolve
deferred._reject = _reject
return deferred
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment