在javascript中展平承诺

Geo*_*mms 11 javascript promise ecmascript-6 es6-promise

蓝鸟图书馆似乎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)
});
Run Code Online (Sandbox Code Playgroud)

这是es6 Promise API的合同吗?例如,我在这里这里都没有提到这种扁平化行为.

Ber*_*rgi 13

这是es6 Promise API的合同吗?

是的,它是由Promises/A +建立的合同,并从那里进入ES6规范.你会发现一些讨论在这里,这里这里.