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

    p1.then(
        function() {
            //slideScroll();
            alert(1);
    });
}

testPromise();

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 1 month ago

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

2 Answers
Best Answer
Sweet stewed snow pear answered 1 month ago

http://liubin.org/promises-book/
Recommend a promise Mini Book
Promise.all should solve your problem

tudewutong answered 1 month 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