javascript中的奉承承诺

Flattening promises in javascript

本文关键字:承诺 javascript      更新时间:2023-09-26

bluebird库似乎自动地将Promise::then用作promise上的"map"answers"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的合同吗?例如,我在这里或这里没有提到这种扁平化行为。

这是es6 Promise API的合同吗?

是的,它是由Promises/a+建立的一个合同,并从那里进入了ES6规范。你会在这里、这里和这里找到一些讨论。