Node.js/Promise

来自康健生活
Kangkk​(留言 | 贡献)2024年8月7日 (三) 14:13的版本 (→并行执行)

标准

Promises/A+ 标准。

方法

then catch all race finally any

状态

fulfilled 已成功
pengding 进行中
rejected 已拒绝

执行状态

并行执行 顺序执行 示例

封装

es6-promise 示例 示例
示例 示例 示例
示例 示例 示例

参数

reslove 示例
reject 示例

串行执行

并行执行

Promise.all

通过 Promise.all 处理异常信息。

模板:Leftrightflex

通过 Promise 任务处理异常信息。

模板:Leftrightflex

示例

标题文字 标题文字 标题文字
习题 示例 示例
示例 示例 示例
示例 示例 示例

传参

1.

promiseDemo.then((result) => {
  console.log(result)
}, (result) => {
  console.log(result)
})

2.

promiseDemo.then((result) => {
  console.log(result)
}).catch((result) => {
  console.log(result)
})

3.

promiseDemo.then((result) => {
  console.log(result)
})   //只接收成功状态

promiseDemo.catch((result) => {
  console.log(result)
})    // 只接收失败状态,可以看成第三种的简写形式

promiseDemo.then(null, (result) => {
  console.log(result)
})   //只接收失败状态

调用

错误链式调用

错误处理机制

实践

使用 try{}catch(){} 捕获 await 抛错