JavaScript native Promise execute callback on both results -
JavaScript native Promise execute callback on both results -
is there way execute callback on both results of promise object?
for illustration want create cleanup logic after execution of xhr request. need this:
var cleanup = function() { something.here(); } mylib.makexhr().then(cleanup,cleanup);
in jquery defered illustration can utilize method always():
mylib.makexhr().always(function() { something.here(); });
does promise back upwards this?
no, there none. discussed spec minimal. doesn't include bunch of other functionality. it's designed interoperate library promises , provide simple functionality.
here right polyfill of proposal made stefpanner.
moreover, disagree current deleted answers adding because they're doing wrong (as enumerable property - no fun). if ignore homecoming values , error state of returned promise. intended way extend native promises subclassing them, sadly, no browsers back upwards yet we'll have wait.
instead of messing native prototypes, should utilize different pattern:
opendb().then(foo).then(bar).finally(close).then(more);
is susceptible forgetting phone call close, if open 100 times in our app, forgetting close 1 time can still devastating. on other hand - can utilize disposer pattern promise libraries provide built in ourselves:
opendb(function(db){ homecoming foo(db).then(bar);// chain here }).then(more);
basically - pattern means instead of having opendb
homecoming promise - have take function and homecoming promise, when function run, if returns promise wait promise resolve. looks like:
function opendb(withdb){ homecoming justopenwithoutcleanup(). then(withdb). then(clean, function(e){ clean(); throw e; }); // note rethrow }
javascript promise
Comments
Post a Comment