Use once
await
to wait for multiple asynchronous processes.
One of the strengths of JavaScript is its ability to perform asynchronous operations to avoid processes waiting for each other. In practical applications, asynchronous operations are often used for processes whose waiting time depends on external factors, such as network connection, storage speed, etc. Here are some examples of asynchronous operations:
In JavaScript, asynchronous operations are usually implemented using functions. A function is a set of blocks of code that perform a specific task, such as an addition function to calculate a sum and a shutdown function to shut down the computer (maybe there is such a function?).
To define an asynchronous function, just add the async
keyword when the function is declared, and the rest is the same as an ordinary function.
For example, we define a function to send emails asynchronously:
<code class="language-javascript">async function kirimEmail(tujuan, judul, isi) { // 发送邮件 // ... }</code>
Or use arrow functions:
<code class="language-javascript">const kirimEmail = async (tujuan, judul, isi) => { // 发送邮件 // ... }</code>
When the above function is called, it will automatically execute asynchronously, which means there will be no waiting for each other.
For example:
<code class="language-javascript">kirimEmail('contoh1@email.com', 'Tes 1 Email', 'Halo. Ini saya lagi ngetes.'); kirimEmail('contoh2@email.com', 'Tes 2 Email', 'Halo. Ini saya lagi ngetes.'); kirimEmail('contoh3@email.com', 'Tes 3 Email', 'Halo. Ini saya lagi ngetes.');</code>
In the above example, all processes that send emails are executed in sequence, but no one process is waited for to complete. Therefore, the next process will be started while the previous process is not completed and will not block each other.
If you need to get data or wait for an asynchronous process to complete, you can use the await
keyword when calling a function.
For example:
<code class="language-javascript">await kirimEmail('contoh1@email.com', 'Tes 1 Email', 'Halo. Ini saya lagi ngetes.'); await kirimEmail('contoh2@email.com', 'Tes 2 Email', 'Halo. Ini saya lagi ngetes.'); await kirimEmail('contoh3@email.com', 'Tes 3 Email', 'Halo. Ini saya lagi ngetes.');</code>
In this example, each process will wait for the previous process to complete. This usually happens when data needs to be fetched from one process for the next process, so one has to wait for the previous process to complete before fetching the required data.
Using Promise.all
, we can call and wait for multiple asynchronous functions at the same time.
An example of using Promise.all
to call multiple asynchronous functions is as follows:
<code class="language-javascript">await Promise.all([ kirimEmail('contoh1@email.com', 'Tes 1 Email', 'Halo. Ini saya lagi ngetes.'), kirimEmail('contoh2@email.com', 'Tes 2 Email', 'Halo. Ini saya lagi ngetes.'), kirimEmail('contoh3@email.com', 'Tes 3 Email', 'Halo. Ini saya lagi ngetes.'), ]);</code>
In the above example, we wait for the completion of three sending mail processes, and the results will be returned in the form of an array regardless of success or failure.
Promise.all
has the following properties:
await
is used; Please refer to MDN's Promise.all
documentation for more details.
Using Promise.all
is very convenient, we only need to write await
? once, and it is also useful if we want to stop all processes if one of them errors. However, if we want to continue executing other processes even if one process fails, we will discuss this in the next article.
Thank you for reading. If you want to discuss, please leave a message. If you want to make friends, please let me know ?
The above is the detailed content of Utilizing Promise.all(). For more information, please follow other related articles on the PHP Chinese website!