The problem of ajax request in for Loop in es6

there is an ajax request in the for loop that asks how to achieve the Synchronize effect without changing the parameter async of the ajax request to false.
where adding the async method to the code in es6 when compiling through gulp will make an error (the reason is unknown for the time being), so is there any other way to achieve the same effect?

var testAjax = async function () {
        for(let i = 0; i < 5; iPP) {
            console.log("test1: " + i);
            var url = "xxx";
            await  $.ajax({
                url: url,
                dataType: "json",
                type: "GET",
                //async: false
            }).then(function() {
                console.log("test2: " + i);
            }).fail(function() {
                console.log("error")
            });
        }
    }
    testAjax();

request output

test1: 0
test2: 0
test1: 1
test2: 1
test1: 2
test2: 2
.
.
.
Oct.27,2021

you can use co + generator, but it is not recommended.

it is best to solve the problem of gulp compilation, but it is not possible to rewrite

with promise.
var testAjax = async function () {
    var promise = Promise.resolve()
    for(let i = 0; i < 5; iPP) {
        promise = promise.then(function(){
            console.log('test1: ' + i);
            let url = "xxx";
            return $.ajax({
                url: url,
                dataType: 'json',
                type: "GET",
                //async: false
            }).then(function() {
                console.log('test2: ' + i);
            })
        })
    }
}
testAjax();

use async.await to meet your needs. Just write the request differently:

var testAjax = function () {
    for(let i = 0; i < 5; iPP) {
    // i
        (function (i) {
            console.log('test1: ' + i);
            var url = "xxx";
              $.ajax({
                url: url,
                dataType: 'json',
                type: "GET",
                //async: false
            }).then(function() {
                console.log('test2: ' + i);
            }).fail(function() {
                console.log('error')
            });
        })(i)
    }
}
testAjax();
Menu