javascript 中的扁平化承诺

Flattening promises in javascript

bluebird 库似乎自动使用 Promise::then 作为 "map" 和 "flatMap" 的等价物,例如,请参阅此示例。

var Promise;

Promise = require('bluebird').Promise;

Promise.resolve(1).then(function(x) {
  return Promise.resolve(x + 1);
}).then(function(x) {
  return console.log(x); // => `2` (not a promise)
});

Promise.resolve(1).then(function(x) {
  return x + 1;
}).then(function(x) {
  return console.log(x); // => `2`
});

Promise.reject('hi').catch(function(x) {
  return Promise.reject('hi2');
}).catch(function(x) {
  return console.error(x); //  => `hi2` (not a promise)
});

这是es6 Promise的契约API吗?例如,我没有看到这种扁平化行为 here or here

Is this a contract of the es6 Promise API?

是的,是Promises/A+, and has made its way from there into the ES6 specification. You will find some discussions here, here and here建立的契约。