Home Wait until all tasks finish in queue to return the value , using async library
Reply: 1

Wait until all tasks finish in queue to return the value , using async library

Emad Dehnavi
1#
Emad Dehnavi Published in 2018-02-14 06:49:05Z

I have used async library to do some tasks concurrently, using the queue, my problem is I update the result inside drain, but when I return the result at end of function, it's still have initial value. should I use promise and async/await again on the queue? this is my code :

result = false;
const q = async.queue((task, callback) => {
console.log('Uploading : ', task.src)
s3.upload({
  Bucket: outputBucket,
  Key: task.desc,
  Body: fs.createReadStream(task.src)
}, callback)
}, PARALLEL_TASK)

q.drain = function() {result= true;}

q.push(fileNamesReformat)

return result; // HERE It's still false!
Emad Dehnavi
2#
Emad Dehnavi Reply to 2018-02-14 07:13:57Z

I find the solution, in case if anyone need, I need to return :

return q.drain()

based on async documentation , drain is a callback that is called when the last item from the queue has returned from the worker

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.338331 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO