javascript - Promise 运行成功和失败回调

标签 javascript jquery promise

我有一系列的 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/

相关文章:

javascript - 使用 jQuery 在代码块中查找和替换?

JQuery Mobile 面板无法导航回首页

javascript - Angular.foreach 异步回调不起作用

javascript - 无法使用resolve(arg) Javascript/Odoo 获取Ajax 结果

javascript - dijit.form.ComboBox 无法与 dojox.data.JsonRestStore 一起使用

javascript - sails 船 "Error: There was an error turning the model into an object."

javascript - 单击图像后如何播放/启动 youtube 视频?

javascript - 将 async/await 与 forEach 循环一起使用

javascript - 从 JS 中的子网生成随机 IP 地址

javascript 获取距边框像素距离的textarea内容