V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
ntdll
V2EX  ›  问与答

关于 NodeJS 与 Electron 的一些疑问

  •  
  •   ntdll · Dec 27, 2021 · 697 views
    This topic created in 1582 days ago, the information mentioned may be changed or developed.

    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  
       Dec 28, 2021   ❤️ 1
    await 所在的 function 必须是声明为 async 的,你这里所在的 function 是上一行的
    then(sample=>{ 的箭头函数,改成
    then(async (sample)=>{ ,

    第二处,await 应该写在 sample.gc()前面
    for (const s of await sample.gc()) {
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5644 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 08:27 · PVG 16:27 · LAX 01:27 · JFK 04:27
    ♥ Do have faith in what you're doing.