Node.js Promise Chain Pain

I’m working with some node scripts that are able to pull data from remote traffic counters. I might be using the wrong tool for the job.

In the process of understanding and troubleshooting, I am simulating the process of remote data collection with just some setTimeouts and debugs in node, as such:

debug('start');
new Promise(function(resolve, reject){
setTimeout(() => resolve(1), 1000);
}).then(function(result){
setTimeout(() => debug(result), 1000);
return result * 2;
}).then(function(result){
setTimeout(() => debug(result), 1000)
return result * 2;
});

And that yielded this when run:

debug('start');
new Promise(function(resolve, reject){
setTimeout(() => resolve(1), 1000);
}).then(function(result){
setTimeout(() => debug(result), 2000);
return result * 2;
}).then(function(result){
setTimeout(() => debug(result), 1000)
return result * 2;
});

Which yielded this when run. Note the order of the answers as well as the times…

It’s interesting how this is working. I’m coming to the conclusion that it’s not the correct tool for what I’m doing (the asynchronous nature of this is making it difficult to avoid flooding modems that have two counters on them).

Comments from Other Sites

Comments are closed.