gpt4 book ai didi

javascript - $q.all - 只取那些已解决的

转载 作者:行者123 更新时间:2023-12-03 12:03:16 26 4
gpt4 key购买 nike

如果是这样的话:

getCol = (colId)->
dfrd = $q.defer()
if colId == "bacon"
dfrd.reject()
else
dfrd.resolve colId
dfrd.promise


getCols = (columns)->
$q.all(_.map(columns, (cs)-> getCol(cs)))


getCols(['eggs','juice']).then (cols)->
console.log cols # works



getCols(['eggs','juice','bacon']).then (cols)->
console.log cols # not even getting here

那么,在 getCols() 中,我怎样才能只返回那些已解决的 promise ?

最佳答案

$q.all 仅在 all of the promises you pass it 时解析已解决。例如,它非常适合仅在加载所有 4 个小部件后才显示仪表板。

如果您不希望出现这种行为,也就是说,您想尝试显示尽可能多的可以成功解析的列,则必须使用不同的方法。

loadedColumns = []

getCols = (columns) ->
for col in columns
willAdd = addColumn(col) # add column needs to store columns in the "loadedColumns" area, then resolve
willAdd.then buildUI
willAdd.catch logError


# Because this method is debounced, it'll fire the first time there is 50 ms of idleness
buildUI = _.debounce ->
// Construct your UI out of "loadedColumns"
, 50

关于javascript - $q.all - 只取那些已解决的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25296960/

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