Kendall Kelly
Kendall Kelly

Reputation: 121

javascript retry async await

I have a multiple async functions which all send a request to a server, and if there's a error they catch it then retry the function, these functions rely on the data from the previous function, so they have to send one after the other, the problem is whenever I call these functions and there's a error, it keeps retrying it like I want, but it goes on to the next function after, instead of waiting waiting for the previous one to return the resolved response.

const request1 = async () => {
    try {
        const data = await rp.get(link, options)
        return data
    } catch (err) {
        request1()
    }

}

const request2 = async (data) => {
    try {
        const data = await rp.get(link, options)
        return data
    } catch (err) {
        request2()
    }

}

const getData = async() => {
await request1()
await request2()

})

getData()

whenever I call the getData() function, it awaits the first request, but even if it has an error, it moves on the second request right after, instead of waiting for the first request to resolve, also I need a try catch for all the request i send instead of one, because if there is a error i just want to retry that one step, not the full thing

Upvotes: 1

Views: 2485

Answers (1)

TKoL
TKoL

Reputation: 13892

You're not returning the re-call

const request1 = async () => {
    try {
        const data = await rp.get(link, options)
        return data
    } catch (err) {
        return await request1(); // i'm not sure if you need await here or not, worth testing
    }

}

If you don't return from the re-call, then what you're doing is essentially identical to this

const request1 = async () => {
    try {
        const data = await rp.get(link, options)
        return data
    } catch (err) {
        request1(); // this does request 1 WITHOUT waiting for a result
    }
    return undefined;    
}

Edit: this first one is a toy example of what happens if you don't return anything

const rp = {
    get: async function() {
        await new Promise(r => setTimeout(r, 250));
        this.count++;
        if (this.count % 2 === 0) {
            return this.count;
        } else {
            throw new Error('error: even')
        }
    },
    count: 0
};

const request1 = async () => {
    try {
        const data = await rp.get();
        console.log('data in request1', data);
        return data
    } catch (err) {
        request1();
    }
};

const request2 = async (data) => {
    try {
        const data = await rp.get();
        console.log('data in request2', data);
        return data
    } catch (err) {
        request2();
    }

};

const getData = async() => {
    console.log('starting request 1');
    await request1();
    console.log('starting request 2');
    await request2()

};

getData();

And this one is what happens when you return:

const rp = {
    get: async function() {
        await new Promise(r => setTimeout(r, 250));
        this.count++;
        if (this.count % 2 === 0) {
            return this.count;
        } else {
            throw new Error('error: even')
        }
    },
    count: 0
};

const request1 = async () => {
    try {
        const data = await rp.get();
        console.log('data in request1', data);
        return data
    } catch (err) {
        return request1();
    }
};

const request2 = async (data) => {
    try {
        const data = await rp.get();
        console.log('data in request2', data);
        return data
    } catch (err) {
        return request2();
    }

};

const getData = async() => {
    console.log('starting request 1');
    await request1();
    console.log('starting request 2');
    await request2()

};

getData();

You'll notice in the first example that request2 starts before request1 logs its data, but in the second example, with the return statements, request2 doesn't start until after request1 gets the data.

Upvotes: 5

Related Questions