问题 在javascript中展平承诺


蓝鸟图书馆似乎是自动使用的 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的合同吗?我没有提到这种扁平化的行为 这里 要么 这里, 例如。


5916
2018-06-28 19:48


起源

呃,那些文档是 非常 疏。 MSDN甚至没有提到这一点 then 返回一个承诺: - / - Bergi


答案:


这是es6 Promise API的合同吗?

是的,这是一份由合同建立的合同 承诺/ A +,并从那里进入ES6规范。你会发现一些讨论 这里这里 和 这里


12
2018-06-28 20:55