6

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);
});
dmansfield
  • 1,108
  • 10
  • 22
  • If any function returns nothing, or simply does not explicitly return, the value that is returned is `undefined`. This is normal Javascript behaviour, and has nothing to do with Promises. – ndugger Feb 03 '16 at 16:37

2 Answers2

7

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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Thanks for the link. For some reason I thought a caller could tell the difference in javascript between a void return and an explicit `return undefined` but it's clearly not the case as per the spec. – dmansfield Feb 03 '16 at 19:34
  • For that matter, also [this question](http://stackoverflow.com/questions/20915450/why-javascript-functions-always-return-a-value) answers it pretty effectively. – dmansfield Feb 03 '16 at 19:40
2

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.

hkasera
  • 2,118
  • 3
  • 23
  • 32