使用async/await 总结

async/await

异步与同步

  1. 同步:同一线程之中,顺序完成不同的操作,是一种阻塞模式;后一个请求操作需要等待前一个操作完成之后,才能发出。

  2. 异步:需要开启不同的线程,是非阻塞模式。

    同步类似于打电话,而异步相当于发短信。

async/await基础使用

发送请求获取天气异步操作,利用node-fetch完成请求

const fetch = require("node-fetch");

async function getWeather(city) {
    const url = `http://www.mxnzp.com/api/weather/current/${encodeURI(city)}`;
    const response = await fetch(url)
    const {data} = await response.json()
    console.log(data)
}

getWeather('深圳')

async/await搭配then使用

const fetch = require("node-fetch");

async function getWeather(city) {
    const url = `http://www.mxnzp.com/api/weather/current/${encodeURI(city)}`;
    const response = await fetch(url)
    return await response.json()
}

getWeather('深圳')
    .then(({data}) => {
        console.log(`城市:${data.address}`)
        console.log(`天气:${data.weather}`)
    })

async/await作用于对象

理论上可以作用在所有的对象上,但是非Promise对象都会通过Promise.resolve()转成Promise对象

const fetch = require("node-fetch");

class APIWeather {
    async getCurrentWeather(city) {
        const url = `http://www.mxnzp.com/api/weather/current/${encodeURI(city)}`
        const response = await fetch(url)
        return await response.json()
    }
}
;(async () => {
    const cityWeatherApi = new APIWeather()
    const cityWeather = await cityWeatherApi.getCurrentWeather('天津')
    console.log(`城市:${cityWeather.data.address}`)
    console.log(`天气:${cityWeather.data.weather}`)
})()

async/await串行与并行

  1. 串行是指多个任务时,各个任务按顺序执行,完成一个之后才能进行下一个。
  2. 并行指的是多个任务可以同时执行。异步是多个任务并行的前提条件。并行用时更短。

    并行的方式

    1. 将请求先赋给变量,await请求变量
    2. 通过Promise.all(),传入一个请求数组
const fetch = require('node-fetch')

const sleep = (timeout = 2000) => new Promise(resolve => {
    setTimeout(resolve, timeout)
})

const getWeather = async (city) => {
    await sleep()
    const url = `http://www.mxnzp.com/api/weather/current/${encodeURI(city)}`
    const response = await fetch(url)
    return await response.json()
}

const showWeather = async () => {
    console.time('showWeather')
    // 串行
    // const ty = await getWeather('太原')
    // const sy = await getWeather('三亚')

    // 并行 (运行时间约为串行一半)
    // 并行 方法一
    // const tyPromise = getWeather('太原')
    // const syPromise = getWeather('三亚')
    // const ty = await tyPromise
    // const sy = await syPromise

    // 并行 方法二
    const [ty, sy] = await Promise.all([
        getWeather('太原'),
        getWeather('三亚')
    ])

    console.log('并行2')

    console.log(`城市:${ty.data.address}`)
    console.log(`天气:${ty.data.weather}`)

    console.log(`城市:${sy.data.address}`)
    console.log(`天气:${sy.data.weather}`)

    console.timeEnd('showWeather')
}

showWeather()

async/await 循环并行处理

const fetch = require('node-fetch')
const bluebird = require('bluebird')

const getWeather = async (city) => {
    await bluebird.delay(2000)
    const url = `http://www.mxnzp.com/api/weather/current/${encodeURI(city)}`
    const response = await fetch(url)
    return await response.json()
}

const showWeather = async () => {
    console.time('showWeather')
    // loop multiple
    const citys = ['太原', '三亚']
    const promises = citys.map(v => getWeather(v))
    for(const promise of promises) {
        const city = await promise
        console.log(`城市:${city.data.address}`)
        console.log(`天气:${city.data.weather}`)
    }
    console.timeEnd('showWeather')
}

showWeather()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值