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

async/await functions inside nodejs ThreadWorker #10

Open
ser0ja opened this issue Jan 22, 2023 · 3 comments
Open

async/await functions inside nodejs ThreadWorker #10

ser0ja opened this issue Jan 22, 2023 · 3 comments

Comments

@ser0ja
Copy link

ser0ja commented Jan 22, 2023

Hello. Is it possible to use async/await functions inside nodejs ThreadWorker?

@ser0ja ser0ja changed the title async/await functions inside ThreadWorker async/await functions inside nodejs ThreadWorker Jan 22, 2023
@j-devel
Copy link
Contributor

j-devel commented Jan 24, 2023

Yes! I have just added such an example (see https://github.com/w3reality/async-thread-worker/blob/master/examples/node/simple.js).

@ser0ja
Copy link
Author

ser0ja commented Jan 24, 2023

thanks, but what if I need the main script to not use "wait" and wait for a response? and the data from threads came already after sending of all messages in threads?

result.push(**await** th.sendRequest(payload));

@j-devel
Copy link
Contributor

j-devel commented Jan 25, 2023

what if I need the main script to not use await and wait for a response?

I think that you can keep the Promise's returned by th.sendRequest(payload), and later, access each result however you like, e.g.

    const result = [];
    for (let payload of ['a', 'b', 'c', 'd']) {
        result.push(th.sendRequest(payload));
    }

    setTimeout(() => { // wait for the thread to finish sending all the messages
        result[2].then(x => console.log(x)); // C
        result[1].then(x => console.log(x)); // B

        th.terminate();
    }, 8000);

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

2 participants