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.

8
  • 1
    Refer this stackoverflow.com/questions/51862338/… Commented Aug 15, 2018 at 20:32
  • What kind of error occurs? Commented Aug 15, 2018 at 20:36
  • 4
    axios.get is a already awaitable you do not need all that new Promise stuff Commented Aug 15, 2018 at 20:38
  • The error is due to syntax, there should be function inside promise Commented Aug 15, 2018 at 20:38
  • Why do you need the new promise? Axios will take care Commented Aug 15, 2018 at 20:43

3 Answers 3

3
async function getRequest(url, headers) {
    const res = await axios.get(url, { headers })
    return res;
};
Sign up to request clarification or add additional context in comments.

Comments

2

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

1 Comment

@Roamer-1888 oops, you're right, of course, thanks for catching. Fixed. The anti-pattern is so bad that it twisted my mind :P
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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.