V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
gam2046
V2EX  ›  问与答

关于 NodeJS 与 Electron 的一些疑问

  •  
  •   gam2046 · 2021-12-27 11:38:01 +08:00 · 360 次点击
    这是一个创建于 823 天前的主题,其中的信息可能已经有所发展或是发生改变。

    program.js

    
    class ProgramCli {
        constructor() {
            EXAMPLE.create({ }).then(sample => {
                for await (const s of sample.gc()) { // <- sample.gc()方法返回 AsyncIterable 对象
                    console.log('gc result', s);
                }
                console.log('sample was started');
            })
        }
    }
    
    const _instance = new ProgramCli()
    
    module.exports.ProgramCli = _instance
    

    主进程 index.js

    
    import { ProgramCli } from './require/program';
    

    实际运行时,却提示

    /src/require/program.js: Unexpected token, expected ( (15:16)
      14 |         }).then(sample => {
    > 15 |             for await (const s of sample.gc()) {
         |                 ^
      16 |                 console.log('gc result', s);
      17 |             }
      18 |             console.log('samplewas started');
    

    请教各位大佬们,是因为主线程的限制,还是其他什么原因导致的不能写嘛?以及应该如何正确的书写

    jifengg
        1
    jifengg  
       2021-12-28 08:49:11 +08:00   ❤️ 1
    await 所在的 function 必须是声明为 async 的,你这里所在的 function 是上一行的
    then(sample=>{ 的箭头函数,改成
    then(async (sample)=>{ ,

    第二处,await 应该写在 sample.gc()前面
    for (const s of await sample.gc()) {
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4165 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 05:27 · PVG 13:27 · LAX 22:27 · JFK 01:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.