admin管理员组文章数量:1327687
I'm studying the node.js module async, but I have some problems with the function async.retry.
According to its github docs, the function will continue trying the task until it succeeds or chances are used up. But how can my task tell success or failure?
I tried the code below:
var async = require('async');
var opts = {
count : -3
};
async.retry(5, function (cb, results) {
++this.count;
console.log(this.count, results);
if (this.count > 0) cb(null, this.count);
else cb();
}.bind(opts), function (err, results) {
console.log(err, results);
});
I expect it to run until count === 1
, but it always prints this:
-2 undefined
undefined undefined
So how can I use the function correctly?
I'm studying the node.js module async, but I have some problems with the function async.retry.
According to its github docs, the function will continue trying the task until it succeeds or chances are used up. But how can my task tell success or failure?
I tried the code below:
var async = require('async');
var opts = {
count : -3
};
async.retry(5, function (cb, results) {
++this.count;
console.log(this.count, results);
if (this.count > 0) cb(null, this.count);
else cb();
}.bind(opts), function (err, results) {
console.log(err, results);
});
I expect it to run until count === 1
, but it always prints this:
-2 undefined
undefined undefined
So how can I use the function correctly?
Share edited Mar 11, 2015 at 3:28 Nathan Tuggy 2,24427 gold badges32 silver badges38 bronze badges asked Mar 11, 2015 at 2:39 Yao ZhaoYao Zhao 4,6134 gold badges24 silver badges33 bronze badges 1-
Thank you for showing the use of
bind
in the async task. I was just trying to figure out how to pass arguments. – M. Ali Commented Dec 16, 2015 at 12:23
1 Answer
Reset to default 5You want your else
-branch to fail. For that, you need to pass something to the error parameter; currently you just pass undefined
which signals success - and that's what you get back.
async.retry(5, function (cb, results) {
++this.count;
console.log(this.count, results);
if (this.count > 0) cb(null, this.count);
else cb(new Error("count too low"));
}.bind(opts), function (err, results) {
console.log(err, results);
});
本文标签: javascriptDetermining successfailure with nodejs function asyncretryStack Overflow
版权声明:本文标题:javascript - Determining successfailure with node.js function async.retry - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742220833a2435426.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论