在for循環(huán)中有一個ajax請求,要求不把ajax請求的參數(shù)async改成false的情況下,如何實現(xiàn)同步效果。其中在es6的代碼在通過gulp編譯的時候加入async方法會出錯(原因暫時不明),所以有沒有其他的方式可以實現(xiàn)同樣的效果?var testAjax = async function () { for(let i = 0; i < 5; i++) { 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();要求輸出test1: 0test2: 0test1: 1test2: 1test1: 2test2: 2.
.
.
es6中 for循環(huán)中ajax請求問題
BIG陽
2018-12-09 22:10:27