zoukankan      html  css  js  c++  java
  • 并发执行异步事件,按顺序输出结果

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

        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)
        

  • 相关阅读:
    css基本的东西
    css写一个梯形
    js事件代理
    css的垂直居中
    css的6中居中的方式
    css的小三角实现的方式
    css清楚浮动的几种常用方法
    css中的伪类和伪元素
    js回调
    11.10 chkconfig:管理开机服务
  • 原文地址:https://www.cnblogs.com/xutongbao/p/14876273.html
Copyright © 2011-2022 走看看