Unc*_*dom 2 javascript asynchronous
我只是想像这里async.whilst()看到的那样使用。
这是我的简单代码,取自他们的文档:
var async = require('async');
console.log('start');
async.whilst(
function () { return true; },
function (callback) {
console.log('iteration');
callback();
},
function (err) {
console.log('end');
},
);
Run Code Online (Sandbox Code Playgroud)
当我运行它时,循环不会运行。只有start打印出来。
小智 6
因为您返回 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');
},
);Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
753 次 |
| 最近记录: |