承诺尽pipe拒绝履行
我正在使用蓝鸟解决方法检查承诺的结果,不pipe任何拒绝。 在第二种方法中,我拒绝了我仍然得到的承诺isFulfilled() true。
var Promise = require('bluebird'); Promise.settle([firstMethod, secondMethod]).then(function(results){ console.log(results[0].isFulfilled()); console.log(results[1].isFulfilled()); // console.log(results[1].reason()); }).catch(function(error){ console.log(error); }); var firstMethod = function() { var promise = new Promise(function(resolve, reject){ setTimeout(function() { resolve({data: '123'}); }, 2000); }); return promise; }; var secondMethod = function() { var promise = new Promise(function(resolve, reject){ setTimeout(function() { reject((new Error('fail'))); }, 2000); }); return promise; };
我debugging了你的代码,你的函数中的代码没有被调用。 你需要实际调用的function:)
Promise.settle([firstMethod(), secondMethod()]).then(function (results) { console.log(results[0].isFulfilled()); // prints "true" console.log(results[1].isFulfilled()); // prints "false" console.log(results[1].reason()); // prints "fail" }).catch(function (error) { console.log(error); });
很确定的是, isFulfilled
是指是否完整,不pipe是否已经resolved
或rejected
。
您可以使用类似isRejected
来检查承诺是否被拒绝。
settle
API已被弃用。 参考github链接和这个信息。 使用reflect
API而不是文档中指出的。
其次, 文档以一个例子指出:
使用
settleAll
.reflect()
来实现settleAll
(等到数组中的所有promise都被拒绝或履行)function
var promises = [getPromise(), getPromise(), getPromise()]; Promise.all(promises.map(function(promise) { return promise.reflect(); })).each(function(inspection) { if (inspection.isFulfilled()) { console.log("A promise in the array was fulfilled with", inspection.value()); } else { console.error("A promise in the array was rejected with", inspection.reason()); } });
以上代码说明:
在上面的例子中,作者通过使用map
返回reflect
和检查每个承诺是isRejected
或isFulfilled
迭代承诺数组。