There are some Promises. I want to execute a code line after all promises are done

나의 재물운? 연애운은?

AI가 봐주는 내 사주팔자 - 운세박사

https://aifortunedoctor.com/

There are some Promises. I want to execute a code line after all promi…

영리치 0 904 2023.02.01 09:46

To execute a code line after all promises have been resolved, you can use the Promise.all() method. Promise.all() takes an array of promises as an argument and returns a new promise that resolves when all of the promises in the argument array have resolved. Here's an example:


const promise1 = new Promise((resolve, reject) => {

  setTimeout(() => {

    resolve('Promise 1 resolved');

  }, 1000);

});


const promise2 = new Promise((resolve, reject) => {

  setTimeout(() => {

    resolve('Promise 2 resolved');

  }, 2000);

});


const promise3 = new Promise((resolve, reject) => {

  setTimeout(() => {

    resolve('Promise 3 resolved');

  }, 3000);

});


Promise.all([promise1, promise2, promise3])

  .then((results) => {

    console.log(results);

    // Logs: ["Promise 1 resolved", "Promise 2 resolved", "Promise 3 resolved"]

    console.log('All promises have resolved');

  })

  .catch((error) => {

    console.error(error);

  });




In this example, we have three promises (promise1, promise2, promise3) that resolve after a set amount of time. We pass these promises to the Promise.all() method, which returns a new promise that resolves when all of the promises in the argument array have resolved.

The then method is called on the new promise, and it receives an array of the results from all of the promises. The code inside the then block is executed when all of the promises have resolved, and you can use the resolved values from the promises as needed.

If one of the promises in the argument array is rejected, the promise returned by Promise.all() will be rejected, and the code in the catch block will be executed.




Comments

나의 재물운? 연애운은?

AI가 봐주는 내 사주팔자 - 운세박사

https://aifortunedoctor.com/

Category
Magazine
훈남/훈녀
 
 
 
상점
Facebook Twitter GooglePlus KakaoStory NaverBand