From 8693f52354136f24ec54e5fbfb03e82958a1c2d2 Mon Sep 17 00:00:00 2001 From: Leon Xu <6479451-leonxu260@users.noreply.gitlab.com> Date: Sun, 17 Oct 2021 16:46:42 -0700 Subject: [PATCH] doc: add pizza example --- source/_posts/异步JavaScript.md | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/source/_posts/异步JavaScript.md b/source/_posts/异步JavaScript.md index 7dc01dd..c097826 100644 --- a/source/_posts/异步JavaScript.md +++ b/source/_posts/异步JavaScript.md @@ -170,4 +170,29 @@ function executionFn(resolve, reject) { //回调作为一个参数 } ``` 你可以定义这个函数并将其传递给Promise构造函数 -使用then和catch在Promise对象上串联动作 \ No newline at end of file +使用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); }); +``` \ No newline at end of file