Skip to content

Instantly share code, notes, and snippets.

@phadej
Last active December 17, 2015 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phadej/5585042 to your computer and use it in GitHub Desktop.
Save phadej/5585042 to your computer and use it in GitHub Desktop.
function fin(promise, body) {
return promise.then(function (res) {
return as(body()).then(function() {
return res;
});
}, function(function (err) {
// if body throws something other, that will override current error
return as(body()).then(function() {
throw err;
});
});
}
// use like
fin(openStuff().then(asyncStuff), function(res) {
return closeStuff();
})
.then(doOtherStuff);
@phadej
Copy link
Author

phadej commented May 15, 2013

from https://github.com/kriskowal/q

Promises also have a fin function that is like a finally clause. The final handler gets called, with no arguments, when the promise returned by getInputPromise() either returns a value or throws an error. The value returned or error thrown by getInputPromise() passes directly to outputPromise unless the final handler fails, and may be delayed if the final handler returns a promise.

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