简体   繁体   English

对 express 中的数组执行 function

[英]execute function for an array in express

I am calling a get function to receive something from an API and that works well with just a single item.我正在调用一个 get function 从 API 接收一些东西,并且只适用于单个项目。 But how can I execute the same function for an array of items and send that with express.但是我怎样才能为一组项目执行相同的 function 并用快递发送。

my get function:我得到 function:

    const getSummonerByName = async name => {
    const response = await axios.get("https://euw1.api.riotgames.com/lol/summoner/v4/summoners/by-name/" + name + "?api_key=" + apiKey)
    return response.data
}

my app.get:我的应用程序.get:

const users = ["MSPaint%20Picasso", "Volker%20Racho", "GingerAone"]

app.get("/", async (req, res) => {
    try {
        const summoner = await lol.getSummonerByName(users[0])
        res.send(summoner)
    }  
    catch(e) {
        return e
    }
})

Using promise.all and Array.map使用 promise.all 和 Array.map

const summoner = await Promise.all(users.map(getSummonerByName))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM