【问题标题】:Are these 2 Promise writings equivalent?这 2 篇 Promise 的著作是等价的吗?
【发布时间】:2017-09-10 19:03:03
【问题描述】:

从一本教程书我得到以下代码

  createShoppingList: (store, shoppinglist) => {
    return api.addNewShoppingList(shoppinglist).then(() => {
      store.dispatch('populateShoppingLists')
    }, () => {
      store.commit(types.ADD_SHOPPING_LIST, shoppinglist)
    })
  }

注意 .then() 块后面的逗号

它是否等同于链式 .then() ?

  createShoppingList: (store, shoppinglist) => {
    return api.addNewShoppingList(shoppinglist)
    .then(() => {
      store.dispatch('populateShoppingLists')
    })
    .then(() => {
      store.commit(types.ADD_SHOPPING_LIST, shoppinglist)
    })
  }

或者它只是 .then() 内的一个块? 喜欢:

return api.addNewShoppingList(shoppinglist)
.then(
() => { store.dispatch('populateShoppingLists')}, 
() => { store.commit(types.ADD_SHOPPING_LIST, shoppinglist) }
)

感谢反馈

【问题讨论】:

  • 不,逗号不在 .then() 之后……它在里面…….then 的第二个回调函数是拒绝时调用的函数
  • 好的,谢谢...所以似乎在 thi 代码中有一个错误,因为当解决了 apI.addNewShoppingList 时应该执行 store.commit 和 store.dispatch ......在这个 oredr 中。提交然后分派..)
  • 也许,谁知道呢,没有完整的上下文
  • 是的,但是在书的上下文中,在添加 shoppingList(将其保存到服务器: ShoppingListsResource.save(data) )之后,必须将其提交到本地商店,然后重新显示新的通过调度执行操作“populateShoppingLists”来列出...
  • 第三个 sn-p 与第一个完全相同,只是换行符不同。是的,它显然与第二个不同。

标签: javascript promise


【解决方案1】:

没有

.then(resolved, rejected)

不等于

.then(resolve)
.then(rejected)// :/

类似于:

.then(resolved)    
.catch(rejected)

(还是有区别的是then里面的rejection现在会被catch,而upper版本没有catch)

【讨论】:

  • 不,它更像.catch(rejected).then(resolved),但不完全是。如果您的 resolved() 方法抛出另一个错误,您会注意到差异; rejected 不会在这里抓住它。
  • @thomas 不,那将永远进入 then... :/
  • 但是如果then() 抛出一个错误,catch() 会捕获它,并且你会得到一个已解决的 Promise。使用.then(a,b) 时,a() 中的错误不会被捕获并获得拒绝的 Promise。 IMO .catch(b).then(a) 更接近于.then(a,b),但它仍然不是一回事,因为在这里您会将catch() 块返回的值注入then()。最终then(a,b) 相当于一个条件,即只执行一侧,而.catch(b).then(a).then(a).catch(b) 双方都可以执行。查看我答案中的 sn-p。
  • @bergi 我知道。这就是为什么现在仍然会捕获当时内部的拒绝,而没有捕获更高版本的原因
【解决方案2】:

不,

.then(resolved, rejected)

更像

.catch(rejected)
.then(resolved)

但不完全是,与 .then(resolved, rejected) 一样,此函数的结果被转发给以下 Promise,而不会相互转发。

var resolved = value => `resolve(${value})`;
var rejected = err => `catched(${err})`;
var throwing = () => { throw "thrown Error" };

Promise.resolve(42)
  .then(resolved, rejected)
  .then(v => console.log("then(a,b) => ", v));
  
Promise.reject(42)
  .catch(rejected)
  .then(resolved)
  .then(v => console.log("catch(b).then(a) => ", v));
  
//and that's why .then(a,b) ain't like .then(a).catch(b)
Promise.resolve(42)
  .then(throwing, rejected)  
  .then(v => console.log("then(throw,b) => ", v))
  .catch(err => console.log("then(throw,b) => ", err));


Promise.resolve(42)
  .then(throwing)
  .catch(rejected)  
  .then(v => console.log("then(throw).catch(b) => ", v))
  .catch(err => console.log("then(throw).catch(b) => ", err));
.as-console-wrapper{top:0;max-height:100%!important}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    • 1970-01-01
    相关资源
    最近更新 更多