以一个网页爬虫为例:Async/await,让你Promise更舒服地写Promise

2 篇文章 0 订阅

There’s a special syntax to work with promises in a more comfortable fashion, called “async/await”. It’s surprisingly easy to understand and use.

async/await是一种让开发者更舒服地写Promise的方式。

关于它的用法可以详见下面两篇文档:

下面我以一个网页爬虫代码为例,
代码1:这是传统的Promise写法, then/catch的写法。

// index.js
const axios = require('axios')

const myArgs = process.argv.slice(2)
const branch = myArgs[0]
const normalizeWar = warType => {
    if(warType === "microstrategy") warType = "MicroStrategy"
    return warType
}
const warType = normalizeWar(myArgs[1])
const URL = "https://XXX"
// The 1st request
axios.post(URL, {
    action: 'coreui_Browse',
    method: 'read',
    data: [{
        repositoryName: 'releases',
        node: `com/microstrategy/${branch}/${warType}`
    }],
    type: 'rpc',
    tid: 10
}).then((response) => {
    // Get the URL of the branch
    let wars = response.data.result.data;
    wars = wars.filter(war => war.type === "component")
    let war;
    if(wars.length > 0) {
        war = wars[wars.length - 1]
    }
    if(war === undefined) {

    } else {
    // The 2nd request
        axios.post(URL, {
            action: 'coreui_Browse',
            method: 'read',
            data: [{
                repositoryName: "releases",
                node: `com/microstrategy/${branch}/${warType}/${war.text}`
            }],
            type: "rpc",
            tid: 20
        }).then(response => {
            let links = response.data.result.data
            links = links.filter(link => link.text.match(/^\S+war$/))
            if(links.length > 0){
                const text = links[0].text
                console.log(text)
            }
        })
    }
})

代码2: 这是async/await的写法。相较于代码1写法,这种写法,没有回调,没有链式,是顺序的范式。这种写法的好处是代码更加简洁明了,符合开发者的思维习惯,易于阅读。

// index.js
(async () => {
    const axios = require('axios')
    const myArgs = process.argv.slice(2)
    const branch = myArgs[0]
    const normalizeWar = warType => {
        if(warType === "microstrategy") warType = "MicroStrategy"
        return warType
    }
    const warType = normalizeWar(myArgs[1])
    const URL = "https://XXX"
    // The 1st post request
    let response = await axios.post(URL, {
        action: 'coreui_Browse',
        method: 'read',
        data: [{
            repositoryName: 'releases',
            node: `com/microstrategy/${branch}/${warType}`
        }],
        type: 'rpc',
        tid: 10
    })
    let wars = response.data.result.data;
    wars = wars.filter(war => war.type === "component")
    let war;
    if(wars.length > 0) {
        war = wars[wars.length - 1]
    }
    if(war === undefined) {
        return;
    }
    // The 2nd post request
    response = await axios.post(URL, {
        action: 'coreui_Browse',
        method: 'read',
        data: [{
            repositoryName: "releases",
            node: `com/microstrategy/${branch}/${warType}/${war.text}`
        }],
        type: "rpc",
        tid: 20
    })
    let links = response.data.result.data
    links = links.filter(link => link.text.match(/^\S+war$/))
    if(links.length > 0){
        const text = links[0].text
        console.log(text)
    }
})()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值