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 207 天前 8
毫无意义
|
2
agagega 207 天前
NodeJS 跑出来的结果:
after func define async2 script end promise running async1 start async2 promise end after resolve then start set timeout timeout trigger |
3
mcfog 207 天前 via Android
开飞机是不是得自己会飞啊
|
4
yuankui 207 天前
茴香豆的茴写出来新高度
|
5
yagamil 207 天前
算是 js 的事件循环的基础题吧。 不难
|
6
learningman 207 天前 via Android
有的顺序其实不是语法规定,而是 v8 实现特定的,这种这。也要管吗
|
7
LiTangDingZhen 206 天前
Event Loop?
|
8
shuimugan 206 天前 1
没啥意义,敢这么写的都让我打回去用正规 async function 写法了。
IDE 的坏味道告警出题人真不看是吧,async function 里面不用 await 的话,函数为啥不去掉 async 。 setTimeout 换成 const setTimeout = require('timers/promises').setTimeout 保平安。 以前面试 PHP 技术管理时,也碰到过 PHP 里类似题目,我就写了几个代码里出现能勉强容忍的,剩下的写"不回答,在团队里敢这么写的都会被打回去重写"。然后一面时告诉我,我的笔试题是历史上最高分的。 |
9
Opportunity 206 天前
@shuimugan
> async function 里面不用 await 的话,函数为啥不去掉 async 这个我知道为什么 https://typescript-eslint.io/rules/promise-function-async/ |
10
chegde 206 天前 via iPhone
面试题得考这个
|
11
hitsmaxft 206 天前 via iPhone
没啥意思,不 await 的 promise 就是入队背景执行呗。
这么写很容易出现 bug ,正常代码禁止这么干的 |
12
lyxxxh2 188 天前
我还以为是语音播报的场景
比如: "微信" "收款" "一" "百" "元" ,5 个音频文件顺序播放 这个看得我懵逼... |
13
RainChen128 56 天前
明天要面试了,特地找回来这个几百天前看到的史帖子复习一下,挺绕的
|