1

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

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

5 Comments

THANKS SO MUCH!!! can you please explain why it returns undefined?
Yes: any function that you don't return a value from returns undefined. So since your try-catch is erroring, and you don't return anything in your catch block or afterward, it returns undefined.
You can basically imagine javascript as placing return undefined at the end of EVERY function definition -- if the function doesn't return anything before that last line, then 'undefined' is returned;
so its like that with all functions or just async functions? i assume all, but basically what its doing is once it catches a error, it returns the function which blocks the next line from running until the function is not returned, but the data is returned right?
Yeah, you can take the code I posted and edit it to see that the data it to see that the data is returned. All functions return undefined if you haven't returned anything else.

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.