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

function is not defined when using async function using babel and Typescript in development #63

Open wbusby88 opened 4 years ago

wbusby88 commented 4 years ago

Hi,

I'm using Babel and Typescript in development, which I compile to JS for production. However, when trying to use microjob with an async function working locally I get a _functionName is not defined error. Here is some simple code to reproduce:

//index.ts
import express from "express";
import {job, start} from "microjob";

const app = express();

app.get("/", async function(req: express.Request, res: express.Response) {
    await start();

    const test = async () => {
        return 'test';
    }

    // all the below fail with the async before the function name above
    // const result = await job(test);
    // const result = await job(() => {
    //     return test();
    // }, { ctx: { test }});
    const result = await job(async () => {
        return test();
    }, { ctx: { test }});
    // const result = await job(async () => {
    //     const response = await test();
    //     return response;
    // }, { ctx: { test }});

    res.send(result);
});

const port = 4790;
app.listen(port);
console.log(`view app at http://localhost:${port}`);

My package.json:

{
  "name": "test-microjob",
  "version": "1.0.0",
  "description": "",
  "main": "index.ts",
  "dependencies": {
    "@babel/core": "^7.9.0",
    "@babel/node": "^7.8.7",
    "@babel/plugin-transform-runtime": "^7.9.0",
    "@babel/preset-env": "^7.9.5",
    "@babel/preset-typescript": "^7.9.0",
    "babel-watch": "^7.0.0",
    "express": "^4.17.1",
    "microjob": "^0.7.0"
  },
  "devDependencies": {
    "@types/express": "^4.17.6"
  },
  "scripts": {
    "start": "babel-watch --inspect --extensions \".ts\" index.ts",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

.babelrc

{
  "presets": ["@babel/env","@babel/preset-typescript"],
  "plugins": [
    "@babel/plugin-transform-runtime"
  ]
}

My tsconfig.json

{
  "compilerOptions": {
    "target": "es2017",
    "allowJs": true,
    "skipLibCheck": false,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "commonjs",
    "moduleResolution": "node",
    "isolatedModules": false,
    "jsx": "preserve",
    "outDir": "./build",
    "sourceMap": true
  },
  "include": [
    "index.ts"
  ]
}

Node version 12.6.2

To note: Using an async function passed

Thanks for any help in advance :+1:

usein-abilev commented 3 years ago

Try to change your target parameter to "ES2020" in the tsconfig.json.