Scenario: With Current implementation If call1 is success then Call2 will be called. Call2 is success then Call3 will be called.
Expected:
Once Call1 and Call2 completely done from array of all filedata some code and Call3 needs to be called at the end.I added code outside the loop but it is called before filedata finishes its loop. How exactly execute the some operation code... which needs to be processed with each filedata ajax response value and has to make Call3 only once.
filedata = [1,2,3] ;
$btn.on('click', function () {
//some operation
filedata.forEach(function (item, index) {
var promise = new Promise(function (resolve, reject) {
$.ajax({
type: "POST",
url: "/call1",
data: item,
async: false,
cache: false,
contentType: false,
processData: false,
success: function (data) {
setTimeout(function () {
resolve(data);
}, 1000);
},
error: function (e) {
reject();
}
});
});
promise.then(function (image) {
$.ajax({
type: "POST",
url: "call2",
data: JSON.stringify({
}),
cache: false,
contentType: "application/json",
processData: false,
success: function (data) {
//current: some operation code which will call the func written for call3
},
error: function (e) {
reject();
}
});
}, function (error) {
reject();
});
});
// tried: some operation code which will call the func written for call3
})```