Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript native Promise execute callback on both results

Is there any way to execute callback on both results of Promise object?

For example I want to make some cleanup logic after execution of xhr request. So I need to do something like this:

var cleanUp = function() { something.here(); }
myLib.makeXhr().then(cleanUp,cleanUp);

In jquery Defered for example i can use method always():

myLib.makeXhr().always(function() { something.here(); });

Does Promise support something like this?

like image 861
Yavanosta Avatar asked Oct 29 '14 10:10

Yavanosta


People also ask

Does promise all run promises in parallel?

Promises cannot "be executed". They start their task when they are being created - they represent the results only - and you are executing everything in parallel even before passing them to Promise. all . Promises are executed at the moment of creation.

Does promise all run in parallel JavaScript?

It is an asynchronous, non-blocking language. This means JavaScript doesn't run in parallel but rather runs only one function/promise at a time.

Does promise all run concurrently?

JavaScript's Promise. all() method takes in a parameter of iterable promises runs them concurrently then returns a single Promise that resolves to an array of results of the input promises. This returned Promise will only resolve if all the input promises have been resolved.


1 Answers

No, there is none. It was discussed but the spec is minimal. It doesn't include a bunch of other functionality. It's designed to interoperate well with library promises and to provide simple functionality.

Here is a correct polyfill of that proposal originally made by StefPanner.

Moreover, I disagree with the current now deleted answers adding it themselves because they're all doing it wrong (as an enumerable property - no fun). Even if we ignore what it does to the return values and error state of the returned promise. The intended way to extend native promises is by subclassing them, sadly, no browsers support this yet so we'll have to wait.

Instead of messing with native prototypes, we should use a different pattern:

openDb().then(foo).then(bar).finally(close).then(more);

Is susceptible to us forgetting to call close, even if we open it 100 times in our app, forgetting to close it even once can still be devastating. On the other hand - we can use the disposer pattern which some promise libraries provide built in ourselves:

openDb(function(db){
    return foo(db).then(bar);// chain here
}).then(more);

Basically - this pattern means instead of having openDB return a promise - we have it take a function and return a promise, when the function is run, if it returns a promise we wait for that promise to resolve. It looks something like:

function openDb(withDb){
    return justOpenWithoutCleanUp().
           then(withDb).
           then(clean, function(e){ clean(); throw e; }); // note the rethrow
}
like image 158
Benjamin Gruenbaum Avatar answered Oct 05 '22 08:10

Benjamin Gruenbaum