Skip to content

【Promise第26题】下面代码的输出是什么?

js

function runAsync (x) { const p = new Promise(r => setTimeout(() => r(x, console.log(x)), 1000)) return p } function runReject (x) { const p = new Promise((res, rej) => setTimeout(() => rej(Error: ${x}, console.log(x)), 1000 * x)) return p } Promise.all([runAsync(1), runReject(4), runAsync(3), runReject(2)]) .then(res => console.log(res)) .catch(err => console.log(err))


## 参考答案:

## 解析

.catch是会捕获最先的那个异常,在这道题目中最先的异常就是runReject(2)的结果。

## 结果

// 1s后输出 1 3 // 2s后输出 2 Error: 2 // 4s后输出 4