我有一系列的承诺,它正在运行pass和fail回调。无法弄清楚原因。
checkForLists: function() {
var listCheckPromise = [];
$.each(scmap.lists, function(i, list) {
listCheckPromise[i] = $().SPServices({
operation: "GetList",
listName: list.name,
})
})
$.map(listCheckPromise, function(listPromise, index){
listPromise.then( pass(index), fail(index) )
})
function pass(index) {
var currentList = scmap.lists[index]
console.log("PASS:", currentList.name, 'list already created')
}
function fail(index) {
var currentList = scmap.lists[index]
console.log("FAIL:", currentList.name, 'does not exist. Creating...')
scmap.createList(currentList)
}
}
答案 0 :(得分:4)
“ ......无法找出原因。”
简单...因为你致电
$.map(listCheckPromise, function(listPromise, index){
listPromise.then(
pass(index), // <-- pass!
fail(index)) // <- and fail!
})
您可能想尝试
$.map(listCheckPromise, function(listPromise, index){
listPromise.then(
function(){pass(index);},
function(){fail(index);})
})
答案 1 :(得分:2)
当你写这个
listPromise.then( pass(index), fail(index) )
您立即执行此2个功能,而不是提供参考
你应该写这样的东西
listPromise.then( pass, fail )