1

I am new to Node.js and Javascript, I used npm package retry to send requests to server.

const retry = require('retry');

async function HandleReq() {

//Some code
return await SendReqToServer();
}

async function SendReqToServer() {
 
operation.attempt(async (currentAttempt) =>{
        try {
            let resp = await axios.post("http://localhost:5000/api/", data, options);
            return resp.data;
        } catch (e) {
            if(operation.retry(e)) {throw e;}
        }
    });
}

I get empty response because SendReqToServer returns a promise before function passed to operation.attempt resolves the promise.

How to solve this issue ?

10
  • SendReqToServer doesn't return anything, is this on purpose? Commented Mar 4, 2021 at 14:01
  • 1
    return resp.data returns data to the .attempt callback. You'll want to return operation.attempt(...) to return the value to SendReqToServer. Commented Mar 4, 2021 at 14:03
  • @evolutionxbox SendReqToServer returns a promise. Commented Mar 4, 2021 at 14:07
  • @ThomasSablik Yes, but it resolves immediately after calling operation.attempt() - without actually waiting for anything - since there's basically an implicit return undefined; at the end of the function. Commented Mar 4, 2021 at 14:09
  • @ThomasSablik yes indeed. - OP: SendReqToServer resolves to undefined, I assume this is not on purpose Commented Mar 4, 2021 at 14:09

2 Answers 2

3

The solution to this question depends on operation.attempt. If it returns a promise you can simply return that promise in SendReqToServer, too. But usually async functions with callbacks don't return promises. Create your own promise:

const retry = require('retry');

async function HandleReq() {

//Some code
return await SendReqToServer();
}

async function SendReqToServer() {
 
    return new Promise((resolve, reject) => {
        operation.attempt(async (currentAttempt) => {
            try {
                let resp = await axios.post("http://localhost:5000/api/", data, options);
                resolve(resp.data);
                return resp.data;
            } catch (e) {
                if(operation.retry(e)) {throw e;}
            }
        });
    });
}
Sign up to request clarification or add additional context in comments.

6 Comments

thanks a lot, this works, but the above mentioned issue happens because the callback function passed waits for axios's promise to resolve, but operation.attempt's doesn't, is my understanding correct ?
@AjaySabarish That's correct. operation.attempt is called and it almost immediately finishes. The callback runs asynchronously.
Thanks, but you said if operation.attempt returns a promise, we can return it as such, but how can we be sure that that promise will contain the value returned in it's callback's promise ?
@AjaySabarish You have to read documentation or source code to be see what it actually returns.
oh ok, got it, thanks, but it need not be always the case right, even if operation.attempt returns a promise ?
|
0

Returning operation.attempt() will return resp.data if there were no errors in the function to sendReqToServer(). Currently, you're just returning the resp.data to operation.attempt(). You need to also return operation.attempt()

const retry = require('retry');

async function HandleReq() {

//Some code
return SendReqToServer();
}

async function SendReqToServer() {
 
return operation.attempt(async (currentAttempt) => {
        try {
            let resp = await axios.post("http://localhost:5000/api/", data, options);
            return resp.data;
        } catch (e) {
            if(operation.retry(e)) {throw e;}
        }
    });
}

2 Comments

What if operation.attempt doesn't return a promise? This won't work.
Thanks a lot for responding. operation.attempt doesn't return a promise

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.