I have a call to AWS where I get a response and the callback looks like so:
const handleResponse = async (response) => {
var responseBody = "";
response.on("data", async function (chunk) {
responseBody += chunk;
});
response.on("end", async function (chunk) {
return responseBody;
});
return responseBody;
};
My current attempt is to call this function with an await like such:
const getLoadIds = async (payload) => {
// console.log(payload);
const resp = await handleResponse(payload)
console.log(resp) <--- this is undefined
}
How can I get the response from the response.on("end") call?
Here is the original caller code:
var client = new AWS.HttpClient();
try {
await client.handleRequest(request, null, async (res) => fn(res));
} catch (err) {
fn is an alias to getLoadIds
handleResponseis not doing anything asynchronously... It just immediately returns the empty string.handleResponse. To do this, remove async from the function, then return a promise instead fromhandleResponsethat resolves toresponseBodywhen theendevent occurs. Remove async from your callbacks.