用async/await实现
时间: 2024-01-09 07:22:54 浏览: 71
async和await的用法
以下是使用async/await实现的例子:
```javascript
async function asyncPromiseAll(arr) {
let result = [];
try {
for (let i = 0; i < arr.length; i++) {
let h = await arr[i];
result.push(h);
}
} catch (err) {
// 错误处理
}
return result;
}
// 示例用法
let promises = [Promise.resolve(1), Promise.reject(), Promise.resolve(3)];
asyncPromiseAll(promises).then(res => {
console.log(res); // [1, undefined, 3]
});
```
阅读全文