I have a function defined in a module that is supposed to do a fetch and return the response. I am having trouble returning the response from the fetch. The calling function gets the return value as undefined.
I am new to JavaScript and Node so might need a little bit of hand-holding if you don't mind.
Calling function
async function executeTest() {
try {
const response = await bpc.postLendingApplication(
blendConnection,
loanData
);
console.log(Response from POST Loan: , response);
} catch (error) {
console.log(error);
}
}
Module function doing the fetch request
const fetch = require(node-fetch);
async function postLendingApplication(connection, data) {
console.log(Processing POST Loan.);
await fetch(connection.url, {
method: POST,
headers: connection.headers,
body: data,
}).then(async res => {
console.log(Status: , res.status);
console.log(StatusText: , res.statusText);
console.log(OK: , res.ok);
return await res;
});
}
The console output is:
Processing POST Loan.
Status: 200
StatusText: OK
OK: true
Response from POST Loan: undefined
As you can see, the fetch did what it was supposed to do and if I log the res.json() within the module method, it prints the payload. But I would like to return the error and response from the fetch so the module behaves as a generic method and the processing and error handling is done in the calling method.