Execute asynchronous events concurrently, output results in sequence, total time is the largest number in the array:

    const getData = async (num) => {
      return await new Promise((resolve) = > {
        setTimeout(() = > {
          resolve(num)
        }, num)
      })
    }

    // Execute asynchronous events concurrently, output results in sequence, total time is the largest number in the array
    const order = async (nums) => {
      const promises = nums.map(async num => {
        return await getData(num)
      })
      for (const data of promises) {
        console.log(await data)
      }
      console.log(new Date()}const nums = [3000.2000.1000.5000.5000]
    console.log(new Date())
    order(nums)
    
Copy the code

 

 

 

Execute asynchronous events sequentially, output results sequentially, total time is the sum of the numbers in the array:

   const getData = async (num) => {
      return await new Promise((resolve) = > {
        setTimeout(() = > {
          resolve(num)
        }, num)
      })
    }

    // Execute asynchronous events sequentially, output results sequentially, total time is the sum of the numbers in the array
    const order = async (nums) => {
      for (const num of nums) {
        const res = await getData(num)
        console.log(res)
      }
      console.log(new Date()}const nums = [3000.2000.1000.5000.5000]
    console.log(new Date())
    order(nums)
    
Copy the code