处理承诺拒绝而不拒绝调用它的承诺?

时间:2017-09-18 17:19:22

标签: node.js ecmascript-6 promise

我希望promise catch块不会使promise失败,我只想在它没有出现时返回false!我怎么能这样做?

我尝试在try catch块中包装拒绝,但它不起作用。

https://jsfiddle.net/2fz69ea2/1/

FirstNumbers = [1, 2, 3]
SecondNumbers = [4, 5, 6]
ThirdNumbers = [7, 8, 9]
NestedNumbers = [FirstNumbers, SecondNumbers, ThirdNumbers]
for x in range(0,3): 
    print "["
    for y in range(0,len(NestedNumbers)):
       if y!=0: print ", "
       print NestedNumbers[y][i]
    print "]"

我也想知道这是否意味着我滥用了承诺。请帮忙!

1 个答案:

答案 0 :(得分:2)

否则

.catch(() => {
  return false;
})

是对的。这将解析false的承诺。

你的问题是错字:

 return failing_promise()

failing_promise不是函数,你不能调用它。它应该只是

return failing_promise

var passing_promise = new Promise(function(resolve, reject) {
  resolve('Success');
});

// I want this function to just return false, 
var failing_promise = new Promise(function(resolve, reject) {
  reject('Failure');
 })
.then(() => {
  return true;
})
.catch(() => {
  return false;
})

passing_promise.then(()=>{
  return failing_promise
}).then((ret) => {
  console.log('please print false.. please! ', ret)
})
.catch(() => {
  console.log('I Never want to make it here, but as it stands I do')
})