Skip to content

Instantly share code, notes, and snippets.

@mittsh
Last active October 27, 2017 13:47
Show Gist options
  • Save mittsh/8b627a19944ed66b050de2b8101ad9b1 to your computer and use it in GitHub Desktop.
Save mittsh/8b627a19944ed66b050de2b8101ad9b1 to your computer and use it in GitHub Desktop.
function futurePromise() {
var _resolve = null;
var _reject = null;
this.promise = new Promise(function(resolve, reject) {
_resolve = resolve;
_reject = reject;
});
this.then = function() {
return this.promise.then(...arguments);
}
this.catch = function() {
return this.promise.catch(...arguments);
}
this.resolve = function() {
_resolve(...arguments);
}
this.reject = function() {
_reject(...arguments);
}
}
// create a promise
var p = new futurePromise();
// wait for it (multiple times if needed)
p.then(val => {
console.log('val(1)', val);
})
// wait another time and chain
p.then(val => {
console.log('val(2)', val);
return new Promise(res => {
setTimeout(_ => { res(); }, 1000)
}) })
.then(val => {
console.log('.then(somethingElse)')
})
.catch(err => { console.error('err', err); })
// resolve it whenever you want
setTimeout(() => {
p.resolve({value:true});
}, 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment