doc: add pizza example

This commit is contained in:
Leon Xu committed 2021-10-17 16:46:42 -07:00
1 parent 9f1fd8c8dc
commit 8693f52354
1 file changed
+26 -1
+26 -1
View File
@@ -170,4 +170,29 @@ function executionFn(resolve, reject) { //回调作为一个参数
}
```
你可以定义这个函数并将其传递给Promise构造函数
使用then和catch在Promise对象上串联动作
使用then和catch在Promise对象上串联动作
# 披萨例子
我们可以传递一个值来解析。。。
```javascript
function orderExecutor(resolve, reject) {
console.log('making our pizza...');
setTimeout(function () {
resolve("Here's your pizza!");
}, 5000);
}
let orderPizza = new Promise (orderExecutor);
orderPizza.then(function (value) { console.log(value); });
```
这个值传递给then函数
传递给then的函数可以将值传递给下一个then回调函数。
```javascript
function eat(value) {
return value + ", and now it's gone";
}
let orderPizza = new Promise (orderExecutor);
orderPizza.then(eat).then(function (value) { console.log(value); });
```