let list = []
(
async () => {
for( let i=0; i<arr.length; i++ ){
await axios({
method: 'post',
url: '/user/12345',
data: {}
}).then(function(res) {
if (res.code == 10001) {
list = [...list,...res.list]
}
});
}
console.log(list)
}
)();
- async 函数返回一个 Promise 对象,可以使用 then 方法添加回调函数。
- async 函数中如果有 await 表达式,async 函数执行时,如果遇到 await 就会先暂停执行 ,等到触发的异步操作完成后,恢复 async 函数的执行并返回解析值。
- await 关键字仅在 async function 中有效。如果在 async function 函数体外使用 await ,你只会得到一个语法错误。
- await 操作符用于等待一个 Promise 对象, 它只能在异步函数 async function 内部使用。