wilk / microjob

A tiny wrapper for turning Node.js worker threads into easy-to-use routines for heavy CPU loads.
https://wilk.github.io/microjob/
MIT License
2.02k stars 47 forks source link

__awaiter is not defined for async jobs #40

Closed nodehack closed 5 years ago

nodehack commented 5 years ago

Synchronous jobs run fine but I'm getting this error when trying to run an async job:

19:51:02.213Z ERROR thingy (1.0.0): Unhandled promise rejection occurred.
    ReferenceError: __awaiter is not defined
        at eval (eval at <anonymous> (/thingy/node_modules/microjob/dist/worker.js:10:5), <anonymous>:8:29)
        at __executor__ (eval at <anonymous> (/thingy/node_modules/microjob/dist/worker.js:10:5), <anonymous>:18:16)
        at MessagePort.<anonymous> (/thingy/node_modules/microjob/dist/worker.js:12:27)
        at MessagePort.emit (events.js:196:13)
        at MessagePort.onmessage (internal/worker/io.js:68:8)

Example code:

public static async initialize () : Promise<void> {
        const threadPoolSize: number = 2;
        logger.info(`Initializing ${threadPoolSize} workers`);
        await start({ maxWorkers: threadPoolSize });

        const res: any = await job(async () => {
            const later: any = (delay: number) => new Promise((resolve: any) => setTimeout(resolve, delay));
            let i: number = 0;
            for (i = 0; i < 10; i++) {
                await later(1);
                for (let j: number = 0; j < 100; j++) {
                    for (let k: number = 0; k < 100; k++) {}
                }
            }
            return i;
        });

        logger.info(`Done Initializing`, res);
    }

Any ideas?

wilk commented 5 years ago

Which version of microjob are you using?

I tried that code, compiled with tsc and used with Node.js 10.5 with --experimental-worker flag and it worked.

nodehack commented 5 years ago

I'm using: microjob 0.5.2. Node 12.1.0 with no --experimental-worker flag. It was removed from the docs and this article indicates it is no longer needed: https://wanago.io/2019/05/06/node-js-typescript-12-worker-threads/

I switch my node version to 10.15.3 and added the flag and still have the same issue. Wondering if this is caused by ts-node which I use when developing. What command are you using to run the code?

nodehack commented 5 years ago

Hmm...looks like the same thing when running using just node. node --experimental-worker server.js

{
    "name": "default-app-name",
    "hostname": "something.local",
    "pid": 18637,
    "level": 50,
    "err": {
        "message": "__awaiter is not defined",
        "name": "Error",
        "stack": "ReferenceError: __awaiter is not defined\n    at eval (eval at parentPort.on (thingy/node_modules/microjob/dist/worker.js:10:5), <anonymous>:8:29)\n    at __executor__ (eval at parentPort.on (thingy/node_modules/microjob/dist/worker.js:10:5), <anonymous>:18:16)\n    at MessagePort.parentPort.on (thingy/node_modules/microjob/dist/worker.js:12:27)\n    at MessagePort.emit (events.js:189:13)\n    at MessagePort.onmessage (internal/worker.js:84:8)"
    },
    "msg": "Unhandled promise rejection occurred.",
    "time": "2019-05-08T14:43:09.184Z",
    "v": 0
}

Here is my tsconfig:

{
    "include": [
        "./src/**/*.ts"
    ],
    "exclude": [
        ".tmp",
        "dist",
        "public",
        "node_modules"
    ],
    "compileOnSave": false,
    "compilerOptions": {            
        "noEmitHelpers": false,
        "allowJs": true,
        "esModuleInterop": true,
        "target": "ES2015",
        "module": "commonjs",
        "moduleResolution": "Node",
        "alwaysStrict": true,
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "removeComments": false,
        "noImplicitAny": true,
        "suppressImplicitAnyIndexErrors": true,
        "outDir": "dist",
        "lib": [
            "es2017",
            "dom",
            "esnext"
        ],
        "typeRoots": [
            "node_modules/@types",
            "types"
        ]
    }
}
nodehack commented 5 years ago

Found it! It was a tsconfig issue. Looks like you must have your target set to es2017.

I removed lib from the compilerOptions and set target to es2017. Works!