javascript - ES6 promise settled callback? -


i want run same action whether promise resolved or not. don't want bind same function both args of .then. isn't there .always jquery has? if not, how achieve this?

isn't there .always jquery has?

no, there's not (yet). though there active proposal, maybe es2018.

if not, how achieve this?

you can implement finally method this:

promise.prototype.finally = function(cb) {     const res = () =>     const fin = () => promise.resolve(cb()).then(res)     return this.then(fin, fin); }; 

or more extensively, passing resolution information callback:

promise.prototype.finally = function(cb) {     const res = () =>     return this.then(value =>         promise.resolve(cb({state:"fulfilled", value})).then(res)     , reason =>         promise.resolve(cb({state:"rejected", reason})).then(res)     ); }; 

both ensure original resolution sustained (when there no exception in callback) , promises awaited.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -