Can someone explain to me why the function below is returning before retrieve_s3_file() finishes?
The client receives no data from response.send below. The data returned from retrieve_s3_file() is correct (based on console output), but a response with no data is sent back to client first.
app.post("/getJobStatus", function(request, response){
var guid = request.body.job_id;
var to_do_list = [];
var data_list = [];
var completed_list = [];
axios.get(job_queue_url "/list?guid=" guid)
.then(function (res){
console.log("Response from API gateway : ");
console.log(res.data);
files = res.data;
for (let i = 0; i < files.length; i )
{
if (files[i] in completed_list) {continue}
else {
to_do_list.push(files[i]);
completed_list.push(files[i]);
}
}
response.send(retrieve_s3_file(to_do_list, guid));
})
.catch(function (error){
console.log(error);
});
CodePudding user response:
If retrieve_s3_files
is asynchronous you will need to wait for the promise to resolve before you can use the value. You can change that last line to something like this:
retrieve_s3_file(to_do_list, guid).then(val => {
response.send(val);
}).catch(error => {
console.log(error)
});
I have added the .catch
method so you can gracefully handle any errors that occur while interacting with s3.