Run javascript function in parallel

Asked

Viewed 191 times

0

Guys I’ve seen some questions but I did not understand well to be sure, after some researches I saw that it was possible to do this using Promises! So I took a function on Google and I modified some things, but I’m not sure it’s running in parallel, can you tell me if I’m right? My job is this::

function someCall() {
  console.log('texto 1')
}

function anotherCall() {
  console.log('2');
}
console.time('teste1')
Promise.allSettled([someCall(), anotherCall()]);
console.timeEnd('teste1')

I want to know if the functions are being performed at the same time

My question may be simple but I’m very confused about it, how do I know if it’s being executed at the same time?

  • No, what I need is for several functions of my application to run at the same time, not a few more, some functions are simple things, but I want to run in parallel. I gave this function that I made an example because if I am correct I will be able to do the rest by myself, can take away the doubt, if this function is running in parallel?

  • In Javascript nothing runs at the same time. Asynchronous execution is something else. To run something really parallel, only using Workers, but this is for specific cases. Are you not trying to make a premature optimization?

  • 1

    For explanation of the functioning of asynchronous functions see https://answall.com/a/16960

1 answer

2


The documentation says the following:

allSettled

The method Promise.allSettled() returns a promise that is resolved after all given promises have been resolved or rejected, with a array of objects that describe the result of each promise.

That is, in your case it will not work the way you expect because its functions are not asynchronous.

To achieve the desired result you should use functions that return promises:

const executar = async (texto, ms) => {
  await aguardar(ms);
  console.log(texto);
};

const aguardar = ms => new Promise(resolve => setTimeout(resolve, ms));

(async () => {
  console.time('Execução');
  await Promise.allSettled([executar('primeiro', 50), executar('segundo', 10)]);
  console.timeEnd('Execução');
})();

Note that in the example above I put a function aguardar which aims to change the time of execution of the function, highlighting the difference of the executions.


Asynchronous functions

The statement async Function defines an asynchronous function, which returns an object AsyncFunction.

You can also define asynchronous functions using a expression async function.

When an asynchronous function is called, it returns a Promise. When the asynchronous function returns a value, a Promise will be solved with the value returned. When the asynchronous function throws an exception or some value, the Promise will be rejected with the value launched.

An asynchronous function may contain an expression await, which pauses the execution of the asynchronous function and waits for the resolution of the Promise passed, and then resumes the execution of the asynchronous function and returns the solved value.

  • I do not understand very well what the function wait can explain me better?

  • If you change the time of the first and second that is 50 to first, and 10 to the second to 0 in the two still yes will run simultaneously?

  • @user210827 yes. The wait function serves only to make it clear that the two functions perform at the same time

  • Okay, but is there any way I can do it without setTimeout? How would I look ? Sorry I’m a little layy with asynchronous programming.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.