本网站(662p.com)打包出售,且带程序代码数据,662p.com域名,程序内核采用TP框架开发,需要联系扣扣:2360248666 /wx:lianweikj
精品域名一口价出售:1y1m.com(350元) ,6b7b.com(400元) , 5k5j.com(380元) , yayj.com(1800元), jiongzhun.com(1000元) , niuzen.com(2800元) , zennei.com(5000元)
需要联系扣扣:2360248666 /wx:lianweikj
ES6中Promise、async和await面试题整理
BilyLiang · 139浏览 · 发布于2023-02-22 +关注

promise是每个前端人必须会接触到的一个知识点,下面这篇文章主要给大家介绍了关于ES6中Promise、async和await面试题的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下

学习过程中遇到的一些基础的Promise、async、await面试题整理。

出题目的:

  • 考察 Promise、async、await 的基础

  • 考察队Event Loop、宏任务、微任务的理解

知识点:

  • JS 执行顺序:单线程,自上而下、先同步后异步、先微任务后宏任务

  • new promise() -> Promise.resolve(),触发then

  • new promise((reject)=>{reject()}) -> promise.reject(),触发catch

  • then 和 catch 内部没有 throw new Error 相当于 resolve

  • async function 相当于返回 Promise.resolve()

  • await 后面的代码都是异步的,微任务;setTimeout是宏任务

  • 初始化Promise时,函数内部代码会被立即执行

代码:

考点1:Promise.resolve、Promise.reject执行顺序

Promise.resolve().then(() => {  // 优先寻找then
        console.log(1);
    }).catch(() => {
        console.log(2);
    })
    // 1
Promise.reject().then(() => {  // 优先寻找catch
        console.log(1);
    }).catch(() => {
        console.log(2);
    })
    // 2

考点2:then 和 catch 内部没有 throw new Error() 相当于 resolve

Promise.resolve().then(() => {
        console.log(1);
    }).catch(() => {
        console.log(2);
    }).then(() => {
        console.log(3);
    })
    // 1 3
Promise.reject().then(() => {
        console.log(1);
    }).catch(() => {
        console.log(2);
    }).then(() => {
        console.log(3);
    })
    // 2 3
Promise.reject().then(() => {
        console.log(1);
    }).catch(() => {
        console.log(2);
        throw new Error();
    }).then(() => {
        console.log(3);
    })
    // 2 报错
Promise.reject().then(() => {
        console.log(1);
    }).catch(() => {
        console.log(2);
        throw new Error();
    }).then(() => {
        console.log(3);
    }).catch(() => {
        console.log(4);
    })
    // 2 4

考点3:async function -> 相当于返回一个 Promise.resolve

const res = async function fn() {
    return 100;
}
console.log(res());  // 返回一个resolve状态的Promise对象 Promise {<fulfilled>: 100}
res().then(()=>{
    console.log(0);
}).catch(()=>{
    console.log(1);
})
// 0
 (async function () {
    const a = fn();
    const b = await fn();
    console.log(a);  // Promise {<fulfilled>: 100}
    console.log(b);  // 100
})()

考点4: await 代码执行顺序

async function fn1() {
    console.log("fn1 start");
    await fn2();
    console.log("fn1 end");
}
async function fn2() {
    console.log("fn2 start");
}
console.log("start");
fn1();
console.log("end");
/**
 * 打印顺序:
 * start
 * fn1 start
 * fn2 start
 * end
 * fn1 end
 */
async function fn1() {
    console.log("fn1 start");
    await fn2();
    console.log("fn1 end");
    await fn3();
    console.log("fn3 end");
}
async function fn2() {
    console.log("fn2");
}
async function fn3() {
    console.log("fn3");
}
console.log("start");
fn1();
console.log("end");
/**
 * 打印顺序:
 * start
 * fn1 start
 * fn2
 * end
 * fn1 end
 * fn3
 * fn3 end
 */

考点5:Promise 与 setTimeout 执行顺序

console.log("start");
setTimeout(()=>{
    console.log("setTimeout")
});
Promise.resolve().then(()=>{
    console.log("Promise")
})
console.log("end")
/**
 * 打印顺序:
 * start
 * end
 * Promise
 * setTimeout
 */
async function fn1() {
    console.log("fn1 start");
    await fn2();
    console.log("fn1 end");  // await后面的代码为"微任务代码"
}
async function fn2() {
    console.log("fn2");
}
console.log("start");
setTimeout(()=>{
    console.log("setTimeout");  // 宏任务
});
fn1();
console.log("end");
/**
 * 打印顺序:
 * start
 * fn1 start
 * fn2
 * end
 * fn1 end
 * setTimeout
 */

附:promise与async await结合使用

昨天看了一道字节外包的面试题

 const list = [1, 2, 3];
    const square = num => {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve(num * num);
            }, 1000);
        });
    }
    function test() {
        // 修改这里的代码
        list.forEach(async x => {
            const res = await square(x);
            console.log(res);
        });
    }
    test()

需要修改的是把同步执行的数组替换成换成异步打印。

在测试以后我们可以-验证,forEach和for循环不同的是for循环可以修改数组的值,且forEach取不到具体某一项的值,这里的异步说的是每执行一次数组循环,就执行一步test()方法,

const list = [1, 2, 3];
const square = num => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve(num * num);
        }, 1000);
    });
}
 function test() {
  for(let x of list) {
    var res = await square(x)
    console.log(res)
  }
}
test()


相关推荐

PHP实现部分字符隐藏

沙雕mars · 1312浏览 · 2019-04-28 09:47:56
Java中ArrayList和LinkedList区别

kenrry1992 · 896浏览 · 2019-05-08 21:14:54
Tomcat 下载及安装配置

manongba · 957浏览 · 2019-05-13 21:03:56
JAVA变量介绍

manongba · 953浏览 · 2019-05-13 21:05:52
什么是SpringBoot

iamitnan · 1076浏览 · 2019-05-14 22:20:36
加载中

0评论

评论
我是湖南最靓仔的那条仔,希望来到这里能够交道志同道合的朋友,一起学习,不断进步!!!
分类专栏
小鸟云服务器
扫码进入手机网页