javascript - Promise running both success and failure callbacks -
i have array of promises , it's running both pass , fail callbacks. can't figure out why.
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 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."
simple... because you're calling
$.map(listcheckpromise, function(listpromise, index){ listpromise.then( pass(index), // <-- pass! fail(index)) // <- , fail! })
you want try
$.map(listcheckpromise, function(listpromise, index){ listpromise.then( function(){pass(index);}, function(){fail(index);}) })