Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How to access the value of a promise?

I'm looking at this example from Angular's docs for $q but I think this probably applies to promises in general. The example below is copied verbatim from their docs with their comment included:
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


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

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

1 Answer

vishaljlf39
.then function of promiseB receives what is returned from .then function of promiseA.

here promiseA is returning is a number, which will be available as number parameter in success function of promiseB. which will then be incremented by 1

Login / Signup to Answer the Question.