Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if a promise completes before then is called?

Let's say I have a Promise like this:

var promise = new Promise(function(resolve, reject) {
    // Do some async thing
});
promise.then(function(response) {
    // Then do some other stuff
});

What happens if the async Promise completes before I call .then()? Normally, I'd only have long running tasks in the Promise function, but what if it completes really quickly one time?

like image 753
ConditionRacer Avatar asked Aug 17 '15 20:08

ConditionRacer


People also ask

Can you call then on a promise twice?

If you call then(...) on its promise again, you immediately get the (first) resolved/rejected result. Additional calls to resolve() will not have any effect.

What happens when a promise is fulfilled?

Fulfilled is a state of a Promise. It means that the promise has been resolved and now has its resolved value (using the internal resolve function). The operation represented by the promise has been completed successfully.

What happens when a promise is returned?

Returns a new Promise object that is resolved with the given value. If the value is a thenable (i.e. has a then method), the returned promise will "follow" that thenable, adopting its eventual state; otherwise, the returned promise will be fulfilled with the value.

What happens when a promise is resolved?

A Promise that is resolved with the given value, or the promise passed as value, if the value was a promise object. It may be either fulfilled or rejected — for example, resolving a rejected promise will still result in a rejected promise.


4 Answers

As expected: then callback will get called immediately in this case if then was called after promise has already resolved.

It's easy to test:

var promise = new Promise(function(resolve, reject) {      resolve(123);  });    setTimeout(function() {    promise.then(function(response) {        alert(response);    });  }, 1000)
like image 100
dfsq Avatar answered Oct 01 '22 01:10

dfsq


As others have already pointed out, you can add callbacks with .then before or after the promise has been resolved, and you can even add more than one callback.

These callbacks will be called in the order they were added, but always asynchronously, after the current turn of the event loop. So if the promise has already been resolved when you add a .then, your handler will be called immediately, but in the "ascynchronous sense".

The Promises/A+ spec says:

[...] onFulfilled and onRejected execute asynchronously, after the event loop turn in which then is called, and with a fresh stack.

like image 20
pdenes Avatar answered Oct 01 '22 01:10

pdenes


A promise has state, which means that even after the promise gets fulfilled, you can attach callbacks using .then to it, and they will be called, with the same result as if the promise was fulfilled after they were attached.

Fulfilled is the final state of a successful promise. This means that you can attach more handlers to the fulfilled promise in the future, using the promise as a cache for the original response.

.then() on MDN

then()

Calls one of the provided functions as soon as this promise is either fulfilled or rejected. A new promise is returned, whose state evolves depending on this promise and the provided callback functions.

The appropriate callback is always invoked after this method returns, even if this promise is already fulfilled or rejected. You can also call the then method multiple times on the same promise, and the callbacks will be invoked in the same order as they were registered.

like image 38
Ori Drori Avatar answered Oct 01 '22 02:10

Ori Drori


The then callback will never get called before the promise is resolved, which is what I think you mean by complete. However, if a promise is resolved before it is returned from a function, any additional success callbacks chained after that moment will still be executed. For example,

function getMeAResolvedPromise() {
    var prom = new Promise();
    prom.resolve('some val');
    return prom;
}

...

getMeAResolvedPromise.then(function(result) {
    // this will still be executed
});
like image 45
GPicazo Avatar answered Oct 01 '22 01:10

GPicazo