• 并发执行异步事件,按顺序输出结果


     并发执行异步事件,按顺序输出结果,总耗时为数组中最大的数:

        const getData = async (num) => {
          return await new Promise((resolve) => {
            setTimeout(() => {
              resolve(num)
            }, num)
          })
        }
    
        //并发执行异步事件,按顺序输出结果,总耗时为数组中最大的数
        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)
        

    继发执行异步事件,按顺序输出结果,总耗时为数组中的数字之和:

       const getData = async (num) => {
          return await new Promise((resolve) => {
            setTimeout(() => {
              resolve(num)
            }, num)
          })
        }
    
        //继发执行异步事件,按顺序输出结果,总耗时为数组中的数字之和
        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)
        

  • 相关阅读:
    9-基础练习 十进制转十六进制
    8-十六进制转十进制
    16-acrobat por 简单使用指南
    LightOJ 1245
    LightOJ 1234
    Codeforces 479【E】div3
    Codeforces 479【F】div3
    Codeforces 479【D】div3
    Codeforces 479【C】div3
    Codeforces 479【B】div3
  • 原文地址:https://www.cnblogs.com/xutongbao/p/14876273.html
Copyright © 2020-2023  润新知