大佬教程收集整理的这篇文章主要介绍了node.js – 如何在第一次失败后停止async.queue?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这取决于这些函数的作用,您可以使用seTinterval进行轮询.但是,如果您的问题是如何阻止将其他任务推送到队列中.你可以这样做:
q.push(tasks,function (err) { if (err && !called) { //Will prevent async to push more tasks to the queue,however please note that //whatever pushed to the queue,it will be processed anyway. q.kill(); //This will not allow double calling for the final callBACk called = true; //This the main process callBACk,the final callBACk main(err,results); } });
var async = require('async'); /* This function is the actual work you are trying to do. Please note for example if you are running child processes here,by doing q.kill you will not stop the execution of those processes,so you need actually to keep track the spawned processed and then kill them when you call q.kill in 'pushCb' function. In-case of just long running function,you may poll using seTinterval */ function worker(task,wcb) { setTimeout(function workerTimeout() { if (task === 11 || task === 12 || task === 3) { return wcb('error in processing ' + task); } wcb(null,task + ' got processed'); },Math.floor(Math.random() * 100)); } /* This function that will push the tasks to async.queue,and then hand them to your worker function */ function process(tasks,concurrency,pcb) { var results = [],called = false; var q = async.queue(function qWorker(task,qcb) { worker(task,function wcb(err,data) { if (err) { return qcb(err); //Here how we propagate error to qcb } results.push(data); qcb(); }); },concurrency); /* The trick is in this function,note that checking q.tasks.length does not work q.kill introduced in async 0.7.0,it is just setTing the drain function to null and the tasks length to zero */ q.push(tasks,function qcb(err) { if (err && !called) { q.kill(); called = true; pcb(err,results); } }); q.drain = function drainCb() { pcb(null,results); } } var tasks = []; var concurrency = 10; for (var i = 1; i <= 20; i += 1) { tasks.push(i); } process(tasks,function pcb(err,results) { console.log(results); if (err) { return console.log(err); } console.log('done'); });
以上是大佬教程为你收集整理的node.js – 如何在第一次失败后停止async.queue?全部内容,希望文章能够帮你解决node.js – 如何在第一次失败后停止async.queue?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。