ntdll
V2EX  ›  问与答

关于 NodeJS 与 Electron 的一些疑问

  •  
  •   ntdll · Dec 27, 2021 · 720 views
    This topic created in 1596 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   ·   981 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 20:46 · PVG 04:46 · LAX 13:46 · JFK 16:46
    ♥ Do have faith in what you're doing.