Skip to content

Instantly share code, notes, and snippets.

@ndruger
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save ndruger/85ce43bd5650323e0e73 to your computer and use it in GitHub Desktop.

Select an option

Save ndruger/85ce43bd5650323e0e73 to your computer and use it in GitHub Desktop.
early return on Q
var Q = require('q');
var util = require('util');
var fn = Q.makePromise.prototype;
fn.catchForceReturn = catchForceReturn;
function ForceReturn(value) {
Error.call(this, value);
this.name = "ForceReturn";
this.value = value;
}
util.inherits(ForceReturn, Error);
Q.ForceReturn = ForceReturn;
function catchForceReturn(fn) {
return this
.fail(function(e) {
if (e instanceof ForceReturn) {
return e.value;
} else {
throw e;
}
})
.then(fn);
}
module.exports = Q;
/* Usage
var Q = require('force-return-q');
function testQ() {
return Q()
.then(function() {
throw new Q.ForceReturn('return value');
})
.then(function() {
console.log('will be skipped');
})
.catchForceReturn()
.fail(function(e) {
console.log('fail', e && e.stack);
return 'return fail value';
});
}
testQ()
.done(function(v) {
console.log('done', v);
});
*/
@ndruger
Copy link
Author

ndruger commented May 27, 2015

bluebird has .cancel
http://openmymind.net/Cancelling-Long-Promise-Chains/

kriskowal/q#64

Q will not implement cancellation on promises

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment