我只是尝试使用 async.whilst()
,如 here 所示.
这是我的简单代码,取自他们的文档:
var async = require('async');
console.log('start');
async.whilst(
function () { return true; },
function (callback) {
console.log('iteration');
callback();
},
function (err) {
console.log('end');
},
);
当我运行它时,循环没有运行。只有 start
被打印出来。
最佳答案
因为您返回的是 true,所以没有调用函数 1 的回调。所以你只能看到“开始”。 您可以在下面找到一些信息:
const async = require('async');
let count = 0;
const compareVariable = 10;
console.log('start');
async.whilst(
function functionName1(callbackFunction) {
// perform before each execution of iterFunctionInside, you need a condition(or other related condition) in 2nd params.
callbackFunction(null, count < compareVariable)
},
// this func is called each time when functionName1 invoked
function iterFunctionInside(callback) {
// increase counter to compare with compareVariable
count++;
console.log('iteration', count);
// if you want to show like tick time, you can set timeout here or comment out if you dont want
setTimeout(() => {
callback(null, count);
}, 1000)
},
function (err, n) {
console.log('end');
},
);
关于javascript - Node.js async.whilst() 根本没有执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57246825/