搜索

查看: 3107|回复: 11

[JavaScript] ES6中Promise、async和await面试题整理

[复制链接]
发表于 2023-5-4 11:47:47 | 显示全部楼层 |阅读模式
Editor 2023-5-4 11:47:47 3107 11 看全部
目录
  • 出题目的:
  • 知识点:
  • 代码:
  • 附: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 {: 100}
    res().then(()=>{
            console.log(0);
    }).catch(()=>{
            console.log(1);
    })
    // 0
    (async function () {
            const a = fn();
            const b = await fn();
            console.log(a);  // Promise {: 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()
    总结
    到此这篇关于ES6中Promise、async和await面试题整理的文章就介绍到这了,更多相关ES6 Promise、async、await面试题内容请搜索知鸟论坛以前的文章或继续浏览下面的相关文章希望大家以后多多支持知鸟论坛
  • 回复

    使用道具 举报

    发表于 2023-6-29 19:59:06 | 显示全部楼层
    李志敏 2023-6-29 19:59:06 看全部
    既然你诚信诚意的推荐了,那我就勉为其难的看看吧!知鸟论坛不走平凡路。
    回复

    使用道具 举报

    发表于 2023-6-30 10:56:31 | 显示全部楼层
    123456848 2023-6-30 10:56:31 看全部
    论坛不能没有像楼主这样的人才啊!我会一直支持知鸟论坛
    回复

    使用道具 举报

    发表于 2023-6-30 23:08:01 | 显示全部楼层
    墙和鸡蛋 2023-6-30 23:08:01 看全部
    感谢楼主的无私分享!要想知鸟论坛好 就靠你我他
    回复

    使用道具 举报

    发表于 2023-7-3 10:31:55 | 显示全部楼层
    知足常乐77 2023-7-3 10:31:55 看全部
    楼主太厉害了!楼主,I*老*虎*U!我觉得知鸟论坛真是个好地方!
    回复

    使用道具 举报

    发表于 2023-7-3 11:36:25 | 显示全部楼层
    123456823 2023-7-3 11:36:25 看全部
    楼主,大恩不言谢了!知鸟论坛是最棒的!
    回复

    使用道具 举报

    发表于 2023-7-3 18:03:13 | 显示全部楼层
    麻辣鸡翅 2023-7-3 18:03:13 看全部
    感谢楼主的无私分享!要想知鸟论坛好 就靠你我他
    回复

    使用道具 举报

    发表于 2023-7-4 07:20:50 | 显示全部楼层
    伊索谗言 2023-7-4 07:20:50 看全部
    楼主发贴辛苦了,谢谢楼主分享!我觉得知鸟论坛是注册对了!
    回复

    使用道具 举报

    发表于 2023-7-4 22:20:43 | 显示全部楼层
    我是的十八簿 2023-7-4 22:20:43 看全部
    其实我一直觉得楼主的品味不错!呵呵!知鸟论坛太棒了!
    回复

    使用道具 举报

    发表于 2023-7-6 16:13:00 | 显示全部楼层
    当当当当裤裆坦 2023-7-6 16:13:00 看全部
    论坛不能没有像楼主这样的人才啊!我会一直支持知鸟论坛
    回复

    使用道具 举报

    • 您可能感兴趣
    点击右侧快捷回复 【请勿灌水】
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则 返回列表

    RSS订阅| SiteMap| 小黑屋| 知鸟论坛
    联系邮箱E-mail:zniao@foxmail.com
    快速回复 返回顶部 返回列表