udpate: promise封装异步操作

This commit is contained in:
qianguyihao
2021-05-20 23:46:06 +08:00
parent 40d518a144
commit bcf8ac039f
2 changed files with 280 additions and 240 deletions

View File

@@ -1,6 +1,8 @@
## Promise 的执行顺序
题目 1
### 题目 1
代码举例
```js
const p = new Promise((resolve, reject) => {
@@ -17,4 +19,31 @@ console.log(2);
2
```
我们需要注意的是Promise里的代码整体其实是同步任务会立即执行
我们需要注意的是Promise 里的代码整体其实是同步任务会立即执行
补充上面的代码中如果继续写`p.then()`那么 `then()`里面是不会执行的因为在定义 promise 的时候需要写 resolve调用 promise 的时候才会执行 `then()`
基于此我们再来看下面这段代码
```js
const p = new Promise((resolve, reject) => {
console.log(1);
resolve();
});
console.log(2);
p.then((res) => {
console.log(3);
});
```
打印结果
```
1
2
3
```
name of having such things download name of giving things download