Skip to content

async/await functions inside nodejs ThreadWorker #10

Open

Description

@ser0ja

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

Activity

  1. changed the title [-]async/await functions inside ThreadWorker[/-] [+]async/await functions inside nodejs ThreadWorker[/+] on Jan 22, 2023
  2. j-devel commented on Jan 24, 2023

    @j-devel
    Contributor
  3. ser0ja commented on Jan 24, 2023

    @ser0ja
    Author

    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));

  4. j-devel commented on Jan 25, 2023

    @j-devel
    Contributor

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions