DWQA QuestionsCategory: ProgramFor multiple consecutive Ajax requests, what's wrong with promise in this code?
Zhou Yang Yang asked 8 months ago
function testPromise() {
    var p1 = new Promise(
        function(resolve, reject) {
            (function() {
                for(var i=0;i<4;i++){

        function() {


Getexpert (I) in the for loop sends four consecutive Ajax requests. I want to execute the alert (1) in P1. Then () after the last request is sent;
Now that the first request is still being sent, it’s already alert
How to change it?

zp1996 replied 8 months ago

becauseajaxIt’s asynchronous. Add every requestpromise, not as it is now

2 Answers
Best Answer
Sweet stewed snow pear answered 8 months ago

Recommend a promise Mini Book
Promise.all should solve your problem

tudewutong answered 8 months ago

First of all, your resolve () should be in the callback method of Ajax request. Second, if you want to make an action after multiple AJAX is sent, use promise.all