简体   繁体   中英

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

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!

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

http://caolan.github.io/async/docs.html#queue

// assign a callback
q.drain = function() {
  console.log('all items have been processed');
};

http://caolan.github.io/async/docs.html#QueueObject
drain function a callback that is called when the last item from the queue has returned from the worker.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM