在for循環中有一個ajax請求,要求不把ajax請求的參數async改成false的情況下,如何實現同步效果。其中在es6的代碼在通過gulp編譯的時候加入async方法會出錯(原因暫時不明),所以有沒有其他的方式可以實現同樣的效果?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.
.
.
- 1 回答
- 0 關注
- 502 瀏覽
添加回答
舉報
0/150
提交
取消