Last active
September 4, 2015 11:02
-
-
Save WebReflection/796d1f04b1173fbcfe5a to your computer and use it in GitHub Desktop.
Optionally resolvable, rejectable, and cancelable Promises through abort.
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
function Lie(callback) {'use strict'; | |
// (C) Andrea Giammarchi - MIT Style License | |
// this is now an official npm module https://github.com/WebReflection/dodgy#dodgy- | |
var | |
resolve, reject, abort, | |
lie = new Promise(function (res, rej) { | |
callback(res, rej, function onAbort(callback) { | |
resolve = res; | |
reject = rej; // feel free to use or ignore arguments | |
abort = function abort() { reject(callback.apply(null, arguments)); }; | |
}); | |
}) | |
; | |
if (abort) { // optional opt in. We need to know how to abort | |
lie.resolve = resolve; // if we'd like to also expose how to resolve | |
lie.reject = reject; // or how to reject, so that we'll have full control. | |
lie.abort = abort; // if aborting doesn't require any specific action | |
} // a no-op Object as callback, as example, would do | |
// opt in for abort propagation | |
return (Lie.more || Object)(lie); | |
} |
Update
This is now an official module called dodgy
Lie.more
In case we want to make abortability available down the chain:
Lie.more = function more(lie) {
function wrap(previous) {
return function () {
var l = previous.apply(lie, arguments);
l.resolve = lie.resolve; // comment out if not needed
l.reject = lie.reject; // comment out if not needed
l.abort = lie.abort;
return Lie.more(l);
};
}
if (lie.abort) {
lie.then = wrap(lie.then);
lie.catch = wrap(lie.catch);
}
return lie;
};
It is now possible to chain "All The Things" and live happily ever after.
var lie = new Lie(function (res, rej, onAbort) {
var t = setTimeout(res, 1000, 'OK');
onAbort(function (why) {
clearTimeout(t);
return why;
});
})
.then(
console.log.bind(console),
console.warn.bind(console)
)
.catch(
console.error.bind(console)
);
lie.abort('because');
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Update
This is now an official module called dodgy
Example, how to opt in:
Optionally we could
p.abort();
, orp.resolve(123);
orp.reject(new Error);
and inspect it as such:If we don't need to do anything particular on
.abort()
beside rejecting, we can opt in viaIf we just need a regular Promise, do not invoke
onAbort
or just ignore it.For all possible rants, complains, insults about this proposal, please use the rest of the internet, thank you.