• 请不要在回答技术问题时复制粘贴 AI 生成的内容
leoleoasd
V2EX  ›  程序员

有关 nodejs 中 atomics 的问题

  •  
  •   leoleoasd ·
    leoleoasd · Jul 9, 2018 · 1712 views
    This topic created in 2875 days ago, the information mentioned may be changed or developed.

    业务需要堵塞事件循环,在这里找到了一个用 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 replies    2018-07-09 18:53:21 +08:00
    geekdada
        1
    geekdada  
       Jul 9, 2018
    不要在 Node 里进行这样毁灭性的操作。

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

    请学习 NodeJS 的异步模型。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2869 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 466ea39e · 38ms · UTC 04:40 · PVG 12:40 · LAX 21:40 · JFK 00:40
    ♥ Do have faith in what you're doing.