Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Microjob doesn't work with typescript #75

Open
adam-arold opened this issue Oct 26, 2021 · 1 comment
Open

Microjob doesn't work with typescript #75

adam-arold opened this issue Oct 26, 2021 · 1 comment

Comments

@adam-arold
Copy link

When I try to run the example using typescript:

(async () => {
    try {
        // start worker pool
        await start();

        // this function will be executed in another thread
        const res = await job(async () => {
            let i = 0;
            for (i = 0; i < 1000000; i++) {
                for (let j = 0; j < 1000000; j++) {
                    for (let k = 0; k < 1000000; k++) {
                        http.get("www.google.it");
                    }
                }
            }
            return i;
        });
        console.log(res); // 1000000
    } catch (err) {
        console.error(err);
    }
})();

I get:

ReferenceError: tslib_1 is not defined
    at eval (eval at <anonymous> ([worker eval]:11:5), <anonymous>:8:37)
    at __executor__ (eval at <anonymous> ([worker eval]:11:5), <anonymous>:10:12)
    at MessagePort.<anonymous> ([worker eval]:13:27)
    at MessagePort.[nodejs.internal.kHybridDispatch] (internal/event_target.js:399:24)
    at MessagePort.exports.emitMessage (internal/per_context/messageport.js:18:26)
    at MessagePort.callbackTrampoline (internal/async_hooks.js:130:17)

If I run the code from javascript it works. Is there a solution for this? ☝️

@adam-arold
Copy link
Author

I tried to reduce this and it seems that the problem is the async function. This fails:

try {
    // start worker pool
    await start();

    // this function will be executed in another thread
    const res = await job(async () => {
        return 1;
    });
    console.log(res);
} catch (err) {
    console.error(err);
}

while this successfully returns 1:

try {
    // start worker pool
    await start();

    // this function will be executed in another thread
    const res = await job(() => {
        return 1;
    });
    console.log(res);
} catch (err) {
    console.error(err);
}

I tried looking up the error message but all I found were 2 GitHub issues that were unactionable and closed. Do you have a working example with typescript?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant