Reputation: 1983
Here I'd expect to see 4 and then 5 in the console. I thought that once the first add
resolves, the then
would go through the anonymous function, which returns another add
.
So what am I missing that causes only the first add
to be ran?
Fiddle: http://www.es6fiddle.net/iobmmhs3/
var add = function(a, b) {
return new Promise(function(reject, resolve) {
console.log(a+b);
resolve(a+b);
});
};
add(1,3)
.then(function() {
return add(2,3)
})
Upvotes: 0
Views: 54
Reputation: 10329
Your resolve
and reject
are backwards:
var add = function(a, b) {
return new Promise(function(resolve, reject) {
// ^^^^^^^ ^^^^^^
console.log(a+b);
resolve(a+b);
});
};
add(1,5)
.then(function() {
return add(2,2)
});
Fiddle: http://www.es6fiddle.net/iobm7rb7/
For documentation, see MDN. For future reference, how you could have debugged this without knowing the signature of the function passed to the constructor: in the console, you should have seen an Uncaught in Promise
error, which is indicating that your promise rejected, and didn't have a reject handler.
Upvotes: 4