0
module.exports = (tel = '', body = '', from = {}, to = {}, callback) => {
    var body = async function() { 
        return await makingAPICall();
    };

    console.log(body);
    callback(null,[body].join('\n');
};

function makingAPICall() { 
  // Promise resolving stuff
}

The above code is in one of my files. Currently what is happening is that when I try to print out the variable body, I just get "async function() { \n return await makingAPICall();\n}", which is just the function body. I read some stuff on await and Promises but I am not sure if I am setting up the async function calls correctly. Any help will be appreciated. Thanks!

EDIT:

module.exports = (tel = '', body = '', from = {}, to = {}, callback) => {
    var body = test();
    console.log(body);
    callback(null,[body].join('\n');
};

async function test() {
  return await makingAPICall();
}

function makingAPICall() { 
  // Promise resolving stuff
}

I also tried doing this but this results in an Promise object being returned after calling test(). I am a bit confused on why this is happening even though I am using async functions and await.

UnitingDust
  • 3
  • 1
  • 6
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – CertainPerformance Nov 29 '18 at 07:27
  • You've only declared a function there. If you want to execute it, you have to call it. But there's no need for the `body` function, just do `await makingAPICall();` – CertainPerformance Nov 29 '18 at 07:27

0 Answers0