gpt4 book ai didi

javascript - 这两个 Promise 的写法是等价的吗?

转载 作者:行者123 更新时间:2023-11-30 15:03:33 25 4
gpt4 key购买 nike

从教程书中我得到以下代码

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

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

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

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

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

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

感谢反馈

最佳答案

没有

.then(resolved, rejected)

不等于

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

它非常类似于:

.then(resolved)    
.catch(rejected)

(还是有区别的是then里面的rejection现在会被捕获,而上层版本是不捕获的)

关于javascript - 这两个 Promise 的写法是等价的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46139678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com