简体   繁体   中英

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? Please I'm new to Axios & promise, async syntax => what should i put inside the.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()?

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.

I recommend using p-limit instead. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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