简体   繁体   中英

How call a function sequentially in a for loop with setInterval

I want to execute function in for loop sequentially.

I use node.js, and my code is...

var insertData = [];
var interval = 500;

function working1(nodeID, keyword, cb){
    insertData.push(nodeID);
    console.log('working: '+nodeID);
    // get data and add value to insertData 
}

function working2()
{
  // insert insertData to database
}

function getDeviceValue(nodeID){
    insertData.push(nodeID);
    console.log('getDeviceValue : '+nodeID);
    async.series([
            function(calllback){
                   working1(nodeID, 'A', function(data, err){
                           if (err) console.log(err);
                           else calllback(null, data);
                           });
            },
            function(calllback){
                   working1(nodeID, 'B', function(data, err){
                            if (err) console.log(err);
                            else calllback(null, data);
                            });
            },
    ], function(err, results){
          working2();
    });
}

setInterval(function() { // This should work periodically.
    dbData = [];
    for (var i=1; i<=4; i++)
        getDeviceValue('0000'+i);
}, interval);

when I was execute above code, it working like...

getDeviceValue : 00001
getDeviceValue : 00002
getDeviceValue : 00003
getDeviceValue : 00004
working : 00001
working : 00002
working : 00003
working : 00004

but I want to make it work like...

getDeviceValue : 00001
working : 00001
getDeviceValue : 00002
working : 00002
getDeviceValue : 00003
working : 00003
getDeviceValue : 00004
working : 00004

when I search it, answer is 'use promise'.

How can I use it?

or

Is there any other way?

Solution without promise, using simple javascript.

 var i = 1; var interval = 500; var insertData = []; function working(nodeID){ insertData.push(nodeID); console.log('working: '+nodeID); } function getDeviceValue(nodeID){ insertData.push(nodeID); console.log('getDeviceValue : '+nodeID); working(nodeID);// Changed } function asyncLoop(index) { if(index > 4) return; setTimeout(function() { getDeviceValue('0000'+index); index++; asyncLoop(index); }, interval); } asyncLoop(i); 

You can put the first console.log inside async.series as:

function getDeviceValue(nodeID){
    insertData.push(nodeID);
    async.series([
        function(callback) {
            console.log('getDeviceValue : '+nodeID);
            callback();
        },
        function(calllback){
               working1(nodeID, 'A', function(data, err){
                       if (err) console.log(err);
                       else calllback(null, data);
                       });
        },
        function(calllback){
               working1(nodeID, 'B', function(data, err){
                        if (err) console.log(err);
                        else calllback(null, data);
                        });
        },
    ], function(err, results){
        working2();
    });
}

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