查看“︁Node.js/Promise”︁的源代码
来自康健生活
←
Node.js/Promise
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
== 标准 == [https://promisesaplus.com/ Promises/A+] 标准。 == 方法 == {| class="wikitable" |- | then || catch || all || race || finally || any |} == 状态 == {| class="wikitable" |- | fulfilled || 已成功 |- | pengding || 进行中 |- | rejected || 已拒绝 |} == 执行状态 == {| class="wikitable" |- | [[/并行执行|并行执行]] || [[/顺序执行|顺序执行]] || 示例 |} == 封装 == {| class="wikitable" |- | [[/es6-promise|es6-promise]] || 示例 || 示例 |- | 示例 || 示例 || 示例 |- | 示例 || 示例 || 示例 |} === 参数 === {| class="wikitable" |- | reslove || 示例 |- | reject || 示例 |} == 并行执行 == === Promise.all === 通过 Promise.all 处理异常信息。 {{leftrightflex|<syntaxhighlight lang="javascript"> const taskA = new Promise((resolve, reject) => setTimeout(() => resolve(true), 10) ) .then(result => result); const taskB = new Promise((resolve, reject) => setTimeout(() => reject(false), 10) ) .then(result => result); // Promise.all([taskA, taskB]) .then(data => console.log(data)) .catch(e => console.log(e));</syntaxhighlight>|{{consoleout|<pre>false </pre>}}}} 通过 Promise 任务处理异常信息。 {{leftrightflex|<syntaxhighlight lang="javascript"> const taskA = new Promise((resolve, reject) => setTimeout(() => resolve(true), 10) ) .then(result => result) .catch(e => e); // .then(result => result, error => error); // const taskB = new Promise((resolve, reject) => setTimeout(() => reject(false), 10) ) .then(result => result) .catch(e => e); // Promise.all([taskA, taskB]) .then(data => console.log(data)) .catch(e => console.log(e));</syntaxhighlight>|{{consoleout|<pre>[ true, false ] </pre>}}}} === 示例 === {| class="wikitable" |- ! 标题文字 !! 标题文字 !! 标题文字 |- | [[/习题|习题]] || 示例 || 示例 |- | 示例 || 示例 || 示例 |- | 示例 || 示例 || 示例 |} == 传参 == 1. <syntaxhighlight lang="javascript"> promiseDemo.then((result) => { console.log(result) }, (result) => { console.log(result) })</syntaxhighlight> 2. <syntaxhighlight lang="javascript"> promiseDemo.then((result) => { console.log(result) }).catch((result) => { console.log(result) })</syntaxhighlight> 3. <syntaxhighlight lang="javascript"> promiseDemo.then((result) => { console.log(result) }) //只接收成功状态 promiseDemo.catch((result) => { console.log(result) }) // 只接收失败状态,可以看成第三种的简写形式 promiseDemo.then(null, (result) => { console.log(result) }) //只接收失败状态</syntaxhighlight> == 调用 == === 错误链式调用 === ==== 错误处理机制 ==== === 实践 === 使用 <code>try{}catch(){}</code> 捕获 <code>await</code> 抛错
此页面嵌入的页面:
模板:Consoleout
(
查看源代码
)
模板:Double-columns
(
查看源代码
)
模板:Double-columns/style.css
(
查看源代码
)
返回
Node.js/Promise
。
导航菜单
康健生活
搜索
user-page
通知
user-interface-preferences
个人工具
登录
五一
多读书,多运动,多到远处走走,再看看不一样的世界。