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

async job example #25

Closed larshp closed 5 years ago

larshp commented 5 years ago

Hi, I tried adding the async example as a test case, see https://github.com/larshp/microjob/commit/5dd6c7b5bdf6110c59fe88cf476b15583906877e

It gives Error: __awaiter is not defined, am I doing something wrong, missing any black magic?

image

wilk commented 5 years ago

Good catch! I think this bug has been introduced during the TS refactoring.

wilk commented 5 years ago

This problem is related to TS: the worker thread is run-time JS and it gets some JS-compiled from TS. However, when you compile an async function via TS, you'll get the __awaiter wrapper that's not defined inside the worker thread. I'm trying to figure out how to pass all the compiled TS and not just a little part of it.

wilk commented 5 years ago

After further investigation, I think I need to rewrite microjob back to JS, leaving just the typings definition. Unfortunately, the function in charge of wrapping the async job is created at run-time and this does not fit with TS compilation. As explained here: https://basarat.gitbooks.io/typescript/docs/async-await.html the compilation of async functions is done through generators and that just does not work with native await keyword. Having a mix of run-time JS and compiled TS is basically bad.

ddwwcruz commented 5 years ago

Not really? __awaiter is made because TS attempts to make async/await usable by versions of node that don't have it. In my experience setting target to es2017 works for this sort of thing.

ddwwcruz commented 5 years ago

Also this package requires an experimental part of node anyway. We might as well compile TS to the latest version of JS.

wilk commented 5 years ago

@ddwwcruz great suggestion! Fixed in version v0.3.1 (just shipped) 🎉