0

I'm in the process of converting some functions to async await and need help to solve an error.

Before

const getRequest = (url, headers) => {
    return new Promise((resolve, reject) => {
        axios.get(url, { headers })
            .then((res) => {
                return resolve(res);
            })
            .catch((err) => {
            return reject(err);
        });
});

After

async function getRequest(url, headers) {
    return new Promise(resolve, reject) {
        try {
            const res = await axios.get(url, { headers })
                return resolve(res);
            }
        catch(err){
                return reject(err);
            };
    };
};

I'm getting an error when running the new code, any help would be appreciated.

Roamer-1888
  • 19,138
  • 5
  • 33
  • 44
skphi13
  • 45
  • 2
  • 7

3 Answers3

3
async function getRequest(url, headers) {
    const res = await axios.get(url, { headers })
    return res;
};
Gibolt
  • 42,564
  • 15
  • 187
  • 127
Atul
  • 420
  • 2
  • 10
3

First of all, your original code is badly indented, so let's fix that:

const getRequest = (url, headers) => {
    return new Promise((resolve, reject) => {
        axios.get(url, { headers })
            .then((res) => {
                return resolve(res);
            })
            .catch((err) => {
                return reject(err);
            });
});

Secondly, your original code contains a whole lot of unnecessary code because axios.get is already a promise. This shows lack of understanding on how promises work, please read this list of anti-patterns. You should just return the promise directly. Fixing that:

const getRequest = (url, headers) => {
    return axios.get(url, { headers });
});

Thirdly, converting a function to async, when the function already returns a promise, means nothing more than simply adding the async keyword itself. You do not have to do anything to the function. Final result:

const getRequest = async (url, headers) => {
    return axios.get(url, { headers });
});
Pedro A
  • 3,989
  • 3
  • 32
  • 56
  • @Roamer-1888 oops, you're right, of course, thanks for catching. Fixed. The anti-pattern is so bad that it twisted my mind :P – Pedro A Aug 15 '18 at 21:13
0

First of all you have got a syntax error in declaring a promise.

The getRequest function should look something like this -

async function getRequest(url, headers) {
    return new Promise((resolve, reject) => {
        try {
            const res = await axios.get(url, { headers })
            return resolve(res);
        }
        catch (err) {
            return reject(err);
        };
    });
};

Second, id axios.get(url, { headers }) is waiatable, you do not need to return a promise from the parent funtion. You can simply return await axios.get(url, { headers });

async function getRequest(url, headers) {
    return await axios.get(url, { headers });
};

Example

async function parent() {
    console.log(await child(true));
}


function child(data) {
    return new Promise((resolve, reject) => {
        if (!!data) {
            resolve("resolved!");
        } else {
            reject("rejected!");
        }
    });
}

parent();
planet_hunter
  • 3,866
  • 1
  • 26
  • 39