关于 NodeJS 与 Electron 的一些疑问

2021-12-27 11:38:01 +08:00
 gam2046

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');

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

365 次点击
所在节点    问与答
1 条回复
jifengg
2021-12-28 08:49:11 +08:00
await 所在的 function 必须是声明为 async 的,你这里所在的 function 是上一行的
then(sample=>{ 的箭头函数,改成
then(async (sample)=>{ ,

第二处,await 应该写在 sample.gc()前面
for (const s of await sample.gc()) {

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/824648

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX