128 lines
1.5 KiB
Markdown
128 lines
1.5 KiB
Markdown
|
#promise
|
||
|
|
||
|
## 填空
|
||
|
下面的打印结果是什么
|
||
|
```
|
||
|
const promise = new Promise((resolve, reject) => {
|
||
|
console.log(1)
|
||
|
resolve()
|
||
|
console.log(2)
|
||
|
})
|
||
|
promise.then(() => {
|
||
|
console.log(3)
|
||
|
})
|
||
|
console.log(4)
|
||
|
|
||
|
```
|
||
|
答案 1 2 4 3
|
||
|
|
||
|
|
||
|
下面打印结果是什么
|
||
|
|
||
|
```
|
||
|
const promise = new Promise((resolve, reject) => {
|
||
|
//resolve('success1')
|
||
|
|
||
|
resolve('success2')
|
||
|
reject('error)
|
||
|
})
|
||
|
|
||
|
promise
|
||
|
.then((res) => {
|
||
|
console.log('then: ', res)
|
||
|
})
|
||
|
.catch((err) => {
|
||
|
console.log('catch: ', err)
|
||
|
})
|
||
|
|
||
|
```
|
||
|
答案 then: success1
|
||
|
|
||
|
下面打印结果是什么
|
||
|
|
||
|
```
|
||
|
|
||
|
Promise.resolve(1)
|
||
|
.then((res) => {
|
||
|
console.log(res)
|
||
|
return 2
|
||
|
})
|
||
|
.catch((err) => {
|
||
|
return 3
|
||
|
})
|
||
|
.then((res) => {
|
||
|
console.log(res)
|
||
|
})
|
||
|
|
||
|
```
|
||
|
|
||
|
答案 1 2
|
||
|
|
||
|
下面打印结果是
|
||
|
|
||
|
```
|
||
|
|
||
|
let a = 1;
|
||
|
const promise = new Promise((resolve, reject) => {
|
||
|
setTimeout(() => {
|
||
|
console.log('once')
|
||
|
console.log(a)
|
||
|
resolve(a++)
|
||
|
}, 1000)
|
||
|
})
|
||
|
|
||
|
const start = Date.now()
|
||
|
promise.then((res) => {
|
||
|
console.log(res)
|
||
|
})
|
||
|
promise.then((res) => {
|
||
|
console.log(res)
|
||
|
})
|
||
|
|
||
|
```
|
||
|
答案
|
||
|
once
|
||
|
1
|
||
|
1
|
||
|
|
||
|
下面代码打印结果是
|
||
|
|
||
|
```
|
||
|
async function asyncFn() {
|
||
|
return 'hello world';
|
||
|
}
|
||
|
console.log(asyncFn())
|
||
|
|
||
|
```
|
||
|
答案:
|
||
|
Promise {<resolved>: "hello world"}
|
||
|
|
||
|
|
||
|
下面代码打印结果是
|
||
|
```
|
||
|
|
||
|
async function asyncFn() {
|
||
|
return 1;
|
||
|
}
|
||
|
asyncFn()
|
||
|
.then(res => {
|
||
|
console.log(res);
|
||
|
})
|
||
|
|
||
|
```
|
||
|
答案 1
|
||
|
|
||
|
下面写法是否正确
|
||
|
|
||
|
```
|
||
|
let a = new Promise((res)=>{
|
||
|
console.log(1)
|
||
|
res(1)
|
||
|
})
|
||
|
function name(){
|
||
|
let a = await a;
|
||
|
}
|
||
|
|
||
|
```
|
||
|
答案 错误
|