doc: create create a promise section

This commit is contained in:
Leon Xu committed 2021-10-17 15:41:51 -07:00
1 parent e843128bed
commit ed97c2fcbe
1 file changed
+21 -1
+21 -1
View File
@@ -150,4 +150,24 @@ Promise算是一种契约:
- 满足:披萨来了!!
- 被拒绝: 厨房的奶酪用完了。
更多的Promise,可以看一些这个[Promise文档](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise)
更多的Promise,可以看一些这个[Promise文档](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise)
# 创建一个Promise
| 函数 | 描述 |
| --- | --- |
| let promiseObj = new Promise(executorFn) | 使用executorFn创建一个新的Promise对象 |
| promiseObj.then(onFulfilled, onRejected) | 当Promise被实现(被拒绝)时调用onFulfilled(onReject)函数 |
| promiseObj.catch(callback) | 如果Promise被拒绝(或发生错误),则调用回调函数 |
```javascript
function executionFn(resolve, reject) { //回调作为一个参数
...
if (conditionMet) {
resolve(); // 通过Promise传递对象
} else {
reject(); // 通过Promise传递对象
}
}
```
你可以定义这个函数并将其传递给Promise构造函数
使用then和catch在Promise对象上串联动作