V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
leoleoasd
V2EX  ›  程序员

有关 nodejs 中 atomics 的问题

  •  
  •   leoleoasd ·
    leoleoasd · 2018-07-09 18:40:30 +08:00 · 1316 次点击
    这是一个创建于 2154 天前的主题,其中的信息可能已经有所发展或是发生改变。

    业务需要堵塞事件循环,在这里找到了一个用 c 写的 sleep 库,但是版本和我的 node 不一样,无法兼容 于是使用他建议的以下方法实现 sleep: test.js

    function msleep(n) {
      Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, n);
    }
    function sleep(n) {
      msleep(n*1000);
    }
    console.log(1)
    sleep(1)
    console.log(2)
    

    使用 node test.js 执行成功 成功暂停一秒

    但是加入 electron-vue 的程序中,提示

    App threw an error during load
    ReferenceError: Atomics is not defined
    

    该怎么解决?为什么用 webpack 打包后运行的结果与 node 运行有差异?

    1 条回复    2018-07-09 18:53:21 +08:00
    geekdada
        1
    geekdada  
       2018-07-09 18:53:21 +08:00
    不要在 Node 里进行这样毁灭性的操作。

    > These calls will block execution of all JavaScript by halting Node.js' event loop!

    请学习 NodeJS 的异步模型。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2225 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 00:10 · PVG 08:10 · LAX 17:10 · JFK 20:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.