
const async1 = async (resolve) => { console.log("async1 start") await async2() resolve() console.log("after resolve") } const async2 = async () => { console.log("async2") } console.log("after func define") setTimeout(()=>{ console.log("set timeout") },0) new Promise(async (resolve)=>{ await async2() console.log("promise running") async1(resolve) console.log("promise end") }).then(()=>{ console.log("then start") setTimeout(()=>{ console.log("timeout trigger") },0) }) console.log("script end") 1 weject 2023-10-14 00:48:56 +08:00 毫无意义 |
2 agagega 2023-10-14 01:06:53 +08:00 NodeJS 跑出来的结果: after func define async2 script end promise running async1 start async2 promise end after resolve then start set timeout timeout trigger |
3 mcfog 2023-10-14 07:27:09 +08:00 via Android 开飞机是不是得自己会飞啊 |
4 yuankui 2023-10-14 09:51:25 +08:00 茴香豆的茴写出来新高度 |
5 yagamil 2023-10-14 11:03:21 +08:00 算是 js 的事件循环的基础题吧。 不难 |
6 learningman 2023-10-14 11:22:19 +08:00 via Android 有的顺序其实不是语法规定,而是 v8 实现特定的,这种这。也要管吗 |
7 LiTangDingZhen 2023-10-14 13:28:59 +08:00 Event Loop? |
8 slowgen 2023-10-14 13:53:20 +08:00 没啥意义,敢这么写的都让我打回去用正规 async function 写法了。 IDE 的坏味道告警出题人真不看是吧,async function 里面不用 await 的话,函数为啥不去掉 async 。 setTimeout 换成 const setTimeout = require('timers/promises').setTimeout 保平安。 以前面试 PHP 技术管理时,也碰到过 PHP 里类似题目,我就写了几个代码里出现能勉强容忍的,剩下的写"不回答,在团队里敢这么写的都会被打回去重写"。然后一面时告诉我,我的笔试题是历史上最高分的。 |
9 Opportunity 2023-10-14 14:37:25 +08:00 @shuimugan > async function 里面不用 await 的话,函数为啥不去掉 async 这个我知道为什么 https://typescript-eslint.io/rules/promise-function-async/ |
10 chegde 2023-10-14 18:30:50 +08:00 via iPhone 面试题得考这个 |
11 hitsmaxft 2023-10-15 09:19:16 +08:00 via iPhone 没啥意思,不 await 的 promise 就是入队背景执行呗。 这么写很容易出现 bug ,正常代码禁止这么干的 |