V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
xyxsw2023
V2EX  ›  JavaScript

群友出了一道 JS 异步顺序题😫

  •  
  •   xyxsw2023 · 196 天前 · 1750 次点击
    这是一个创建于 196 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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")
    
    13 条回复    2024-03-13 00:47:39 +08:00
    weject
        1
    weject  
       196 天前   ❤️ 8
    毫无意义
    agagega
        2
    agagega  
       196 天前
    NodeJS 跑出来的结果:
    after func define
    async2
    script end
    promise running
    async1 start
    async2
    promise end
    after resolve
    then start
    set timeout
    timeout trigger
    mcfog
        3
    mcfog  
       196 天前 via Android
    开飞机是不是得自己会飞啊
    yuankui
        4
    yuankui  
       196 天前
    茴香豆的茴写出来新高度
    yagamil
        5
    yagamil  
       196 天前
    算是 js 的事件循环的基础题吧。 不难
    learningman
        6
    learningman  
       196 天前 via Android
    有的顺序其实不是语法规定,而是 v8 实现特定的,这种这。也要管吗
    LiTangDingZhen
        7
    LiTangDingZhen  
       196 天前
    Event Loop?
    shuimugan
        8
    shuimugan  
       196 天前   ❤️ 1
    没啥意义,敢这么写的都让我打回去用正规 async function 写法了。
    IDE 的坏味道告警出题人真不看是吧,async function 里面不用 await 的话,函数为啥不去掉 async 。
    setTimeout 换成 const setTimeout = require('timers/promises').setTimeout 保平安。

    以前面试 PHP 技术管理时,也碰到过 PHP 里类似题目,我就写了几个代码里出现能勉强容忍的,剩下的写"不回答,在团队里敢这么写的都会被打回去重写"。然后一面时告诉我,我的笔试题是历史上最高分的。
    Opportunity
        9
    Opportunity  
       195 天前
    @shuimugan
    > async function 里面不用 await 的话,函数为啥不去掉 async

    这个我知道为什么
    https://typescript-eslint.io/rules/promise-function-async/
    chegde
        10
    chegde  
       195 天前 via iPhone
    面试题得考这个
    hitsmaxft
        11
    hitsmaxft  
       195 天前 via iPhone
    没啥意思,不 await 的 promise 就是入队背景执行呗。
    这么写很容易出现 bug ,正常代码禁止这么干的
    lyxxxh2
        12
    lyxxxh2  
       177 天前
    我还以为是语音播报的场景
    比如: "微信" "收款" "一" "百" "元" ,5 个音频文件顺序播放
    这个看得我懵逼...
    RainChen128
        13
    RainChen128  
       45 天前
    明天要面试了,特地找回来这个几百天前看到的史帖子复习一下,挺绕的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2800 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 06:07 · PVG 14:07 · LAX 23:07 · JFK 02:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.