Sending response in async function

I need to return an array of labels, but I can only return 1 of the labels so far. The error which I get is "Cannot set headers after they are sent to the client". So I tried res.write and placed res.end after my for loop then I get the obvious error of doing a res.end before a res.write. How do I solve this?

for(let i=0;i<arr.length;i++){
request.get(arr[i], function (error, response, body) {
  if (!error && response.statusCode == 200) {
    myfunction();
    async function myfunction(){
    const Labels = await Somefunctioncallwhoseresponseigetlater(body)
    res.send(Labels);
}
}
});}


Read more here: https://stackoverflow.com/questions/64902479/sending-response-in-async-function

Content Attribution

This content was originally published by bigdaddy-zephyr at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: