ES6中Promise、async和await面试题实例代码分析

其他教程   发布日期:2023年06月26日   浏览次数:482

这篇“ES6中Promise、async和await面试题实例代码分析”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“ES6中Promise、async和await面试题实例代码分析”文章吧。

知识点:

  • 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执行顺序

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

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

  1. Promise.resolve().then(() => {
  2. console.log(1);
  3. }).catch(() => {
  4. console.log(2);
  5. }).then(() => {
  6. console.log(3);
  7. })
  8. // 1 3
  1. Promise.reject().then(() => {
  2. console.log(1);
  3. }).catch(() => {
  4. console.log(2);
  5. }).then(() => {
  6. console.log(3);
  7. })
  8. // 2 3
  1. Promise.reject().then(() => {
  2. console.log(1);
  3. }).catch(() => {
  4. console.log(2);
  5. throw new Error();
  6. }).then(() => {
  7. console.log(3);
  8. })
  9. // 2 报错
  1. Promise.reject().then(() => {
  2. console.log(1);
  3. }).catch(() => {
  4. console.log(2);
  5. throw new Error();
  6. }).then(() => {
  7. console.log(3);
  8. }).catch(() => {
  9. console.log(4);
  10. })
  11. // 2 4

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

  1. const res = async function fn() {
  2. return 100;
  3. }
  4. console.log(res()); // 返回一个resolve状态的Promise对象 Promise {<fulfilled>: 100}
  5. res().then(()=>{
  6. console.log(0);
  7. }).catch(()=>{
  8. console.log(1);
  9. })
  10. // 0
  11. (async function () {
  12. const a = fn();
  13. const b = await fn();
  14. console.log(a); // Promise {<fulfilled>: 100}
  15. console.log(b); // 100
  16. })()

考点4: await 代码执行顺序

  1. async function fn1() {
  2. console.log("fn1 start");
  3. await fn2();
  4. console.log("fn1 end");
  5. }
  6. async function fn2() {
  7. console.log("fn2 start");
  8. }
  9. console.log("start");
  10. fn1();
  11. console.log("end");
  12. /**
  13. * 打印顺序:
  14. * start
  15. * fn1 start
  16. * fn2 start
  17. * end
  18. * fn1 end
  19. */
  1. async function fn1() {
  2. console.log("fn1 start");
  3. await fn2();
  4. console.log("fn1 end");
  5. await fn3();
  6. console.log("fn3 end");
  7. }
  8. async function fn2() {
  9. console.log("fn2");
  10. }
  11. async function fn3() {
  12. console.log("fn3");
  13. }
  14. console.log("start");
  15. fn1();
  16. console.log("end");
  17. /**
  18. * 打印顺序:
  19. * start
  20. * fn1 start
  21. * fn2
  22. * end
  23. * fn1 end
  24. * fn3
  25. * fn3 end
  26. */

考点5:Promise 与 setTimeout 执行顺序

  1. console.log("start");
  2. setTimeout(()=>{
  3. console.log("setTimeout")
  4. });
  5. Promise.resolve().then(()=>{
  6. console.log("Promise")
  7. })
  8. console.log("end")
  9. /**
  10. * 打印顺序:
  11. * start
  12. * end
  13. * Promise
  14. * setTimeout
  15. */
  1. async function fn1() {
  2. console.log("fn1 start");
  3. await fn2();
  4. console.log("fn1 end"); // await后面的代码为"微任务代码"
  5. }
  6. async function fn2() {
  7. console.log("fn2");
  8. }
  9. console.log("start");
  10. setTimeout(()=>{
  11. console.log("setTimeout"); // 宏任务
  12. });
  13. fn1();
  14. console.log("end");
  15. /**
  16. * 打印顺序:
  17. * start
  18. * fn1 start
  19. * fn2
  20. * end
  21. * fn1 end
  22. * setTimeout
  23. */

附:promise与async await结合使用

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

  1. const list = [1, 2, 3];
  2. const square = num => {
  3. return new Promise((resolve, reject) => {
  4. setTimeout(() => {
  5. resolve(num * num);
  6. }, 1000);
  7. });
  8. }
  9. function test() {
  10. // 修改这里的代码
  11. list.forEach(async x => {
  12. const res = await square(x);
  13. console.log(res);
  14. });
  15. }
  16. test()

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

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

  1. const list = [1, 2, 3];
  2. const square = num => {
  3. return new Promise((resolve, reject) => {
  4. setTimeout(() => {
  5. resolve(num * num);
  6. }, 1000);
  7. });
  8. }
  9. function test() {
  10. for(let x of list) {
  11. var res = await square(x)
  12. console.log(res)
  13. }
  14. }
  15. test()

以上就是ES6中Promise、async和await面试题实例代码分析的详细内容,更多关于ES6中Promise、async和await面试题实例代码分析的资料请关注九品源码其它相关文章!