简体   繁体   English

如何仅使用 async/await 语法重写 axios promise 模块?

[英]How can i rewrite the axios promise module with only async/await syntax?

How can I rewrite the Axios promise module with only async/await syntax?如何仅使用 async/await 语法重写 Axios promise 模块? Please I'm new to Axios & promise, async syntax => what should i put inside the.push()?请我是 Axios 和 promise 的新手,异步语法 => 我应该在.push() 中放什么?

const axios = require("axios");
const _ = require("lodash");
const queue = require("queue");

// To control the request rate
const GetDataQueue_B = queue({ autostart: true, concur: 1 });
// someone's codes
const getDATA = () => {
    return new Promise((resolve, reject) => {
      // To make sure only one request call
      GetDataQueue_B.push(
        async () => {
            try{
            const res = await axios.get(`${whichAPI}/api`);
            resolve(
                _.map(
                _.get(res, "data.infor"), 
                    obj => ({
                        quotes:obj.quotes,
                        author:boj.author,
                    })
                )
            );
            }catch(e) {reject(e);console.log(e);}
        }
      );
    }); 

below is my try: => what should i put inside the.push()?下面是我的尝试: => 我应该在.push() 中放什么?

const getDATA_1 = async () => {
    try{
    const res = await axios.get(`${whichAPI}/api`);
    const data =  _.map(
                    _.get(res, "data.infor"), 
                        obj => ({
                            quotes:obj.quotes,
                            author:boj.author,
                        })
                    )                 
    GetDataQueue_B.push(); // => what should i put inside the .push() method?
    return data;
    }catch(e){console.log(e);}
  };

queue isn't the best choice here for clean code because while it allows pushing functions that return a promise, it itself doesn't return a promise. queue在这里不是干净代码的最佳选择,因为虽然它允许推送返回 promise 的函数,但它本身不会返回 promise。

I recommend using p-limit instead.我建议改用p-limit It works like this:它是这样工作的:

const pLimit = require("p-limit");

const myQueue = pLimit(5); // Allow only 5 things running in parallel

const promise = myQueue(async () => { /* stuff */ });
// promise will resolve once the passed function ran, and will return its
// result.

So, the code then looks like this:因此,代码如下所示:

const axios = require("axios");
const _ = require("lodash");
const pLimit = require("p-limit");

// To control the request rate
const GetDataQueue_B = pLimit(1);
// someone's codes
const getDATA = () => GetDataQueue_B(async () => {
  const res = await axios.get(`${whichAPI}/api`);
  return _.map(
    _.get(res, "data.infor"), 
      obj => ({
        quotes: obj.quotes,
        author: boj.author,
      })
    )
  );
}); 

I removed the try / catch because it didn't really do anything - an error here would reject the resulting promise anyway.我删除了try / catch ,因为它并没有真正做任何事情——这里的错误无论如何都会拒绝生成的 promise。

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

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