【问题标题】:Flattening promises in javascript扁平化 javascript 中的 Promise
【发布时间】:2015-09-15 05:32:19
【问题描述】:

bluebird 库似乎自动将Promise::then 用作 promise 上的“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 的合约吗?例如,我没有看到提到这种扁平化行为 herehere

【问题讨论】:

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

标签: javascript promise ecmascript-6 es6-promise


【解决方案1】:

这是 es6 Promise API 的合约吗?

是的,它是由Promises/A+ 建立的合约,并已从那里进入 ES6 规范。你会发现一些讨论hereherehere

【讨论】:

    猜你喜欢
    • 2017-12-30
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-08
    相关资源
    最近更新 更多