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

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

如何仅使用 async/await 语法重写 Axios promise 模块?请 我是 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);}
        }
      );
    }); 

下面是我的尝试:=> 我应该在 .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 不是干净代码的最佳选择,因为虽然它允许推送 return 承诺的函数,但它本身并不是 return 承诺,所以你在某些时候总是被迫使用 new Promise 并在其成功回调中或在您自己的代码末尾使用 resolve 它。我想这就是您坚持改进此代码的主要原因。

我建议改用 p-limit。它是这样工作的:

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.

因此,代码如下所示:

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,
      })
    )
  );
}); 

我删除了 try/catch 因为在新版本中它不会真正做任何事情 - 这里的错误无论如何都会拒绝结果承诺。


旁注:此处不需要使用 lodash。将依赖项保持在最低限度并使用 built-in 功能来代替总是好的。我们可以在没有 lodash 的情况下重写代码,如下所示:

const axios = require("axios");
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 res.data?.infor?.map(({ quotes, author }) => ({ quotes, author })) ?? [];
}); 

如果您确定 res.data.infor 数组存在并且您也不关心结果中除 quotesauthor 之外的额外属性,这甚至可以简化进一步:

const getDATA = () => GetDataQueue_B(async () => {
  const res = await axios.get(`${whichAPI}/api`);
  return res.data.infor;
});