简体   繁体   中英

converting functions to async await

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.

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

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

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

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