Aerovistae Aerovistae - 1 year ago 89
Javascript Question

How to access the value of a promise?

I'm looking at this example from Angular's docs for

but I think this probably applies to promises in general. They have this example:

promiseB = promiseA.then(function(result) {
return result + 1;

// promiseB will be resolved immediately after promiseA is resolved and its value
// will be the result of promiseA incremented by 1

But I'm not clear how this works. If I could write
on the result of the first
, chaining them, which I know I can, then
is a promise object, of type
. It is not a
. So what do they mean by "its value will be the result of promiseA incremented by 1"?

Am I supposed to access that as
or something like that? How can the success callback return a promise AND return "result + 1"? I'm missing something.

Answer Source

promiseA's then function returns a new promise (promiseB) that is immediately resolved after promiseA is resolved, its value is the value of the what is returned from the success function within promiseA.

In this case promiseA is resolved with a value - result and then immediately resolves promiseB with the value of result + 1.

Accessing the value of promiseB is done in the same way we accessed the result of promiseA.

promiseB.then(function(result) {
    // here you can use the result of promiseB
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download