dmansfield
dmansfield

Reputation: 1128

What is the resolution value of a chained Promise if a then handler doesn't return anything?

If a then handler has no return statement, the resulting chained promise takes on the value undefined in bluebird. But I cannot see anywhere that it is specified in Promises/A+ or anywhere? Can this behavior be counted on?

Here's a test program:

var Promise = require('bluebird');

var p = Promise.resolve('test');

p.then(function(s) {
    console.log('s1='+s);
    // no return
}).then(function(s) {
    // bluebird prints "undefined".  is this specified by a standard?
    console.log('s2='+s);
});

Upvotes: 6

Views: 428

Answers (2)

Bergi
Bergi

Reputation: 664936

Promises/A+ specifies to use the return value of a callback to resolve the promise.

Every function call that doesn't throw an exception (that has a "normal completion", in spec terms) does have such a return value. If a function execution doesn't encounter a return statement, this value will be undefined. This is made explicit in the spec in section 9.2.1.

Upvotes: 6

hkasera
hkasera

Reputation: 2148

That is an expected behaviour and would happen even if you don't use bluebird. If you don't explicitly resolve with a value, the value is undefined, as is typical in JS.

Refer this link to the book "You Don't know JS". It explains it nicely.

Upvotes: 1

Related Questions