Created
July 9, 2016 02:15
-
-
Save tec27/e2f8776dcf47d31057ca1b229ff7d9f7 to your computer and use it in GitHub Desktop.
An extension of ES6 Promises that allows for easier deferred resolution/rejection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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