我有一系列的 promise ,它正在运行通过和失败回调。不明白为什么。
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)
}
}
最佳答案
"... Can't figure out why."
简单...因为您打电话
$.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);})
})
关于javascript - Promise 运行成功和失败回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37580259/