I have a promise chain that I want to run, and when it finishes, I resolve
the Promise (please see code below for more context), and expect my .then()
block to run... But it does not.
Here is my code:
function notifications_for_each_in_array(iteration, the_array, error_array) {
return new Promise(function(resolve, reject) {
if(!iteration) iteration = 0;
var error_array = error_array || [];
var user_id = the_array[iteration];
$.ajax({
url: ".....my_url.....",
type: "PUT",
data: JSON.stringify({test: [user_id]}),
success: function() {
// ...
}
}).done(function(rez) {
error_array.push(rez);
iteration++;
console.log("will stop: " + (the_array[iteration] == undefined));
if(the_array[iteration] == undefined) { // reached the end
console.log("Resolving...");
resolve(error_array);
} else {
if(the_array[iteration] != undefined) {
console.log("Next: " + iteration);
notifications_for_each_in_array(iteration, the_array, error_array);
}
}
}).fail(function(err) {
console.error(err);
});
});
}
The above function works, but when I call it with a .then()
, the .then()
block does not run.
In this example, I never get alerted (PS: I also tried defining notifications_for_each_in_array
as an async
function, and using await
, but I get the same result):
notifications_for_each_in_array(0, [0,1,2,3], [])
.then(function(res) {
alert("here is then()"); // I never get alerted!
});