如何在Node.js中完成for循环后运行一个函数?
说如果我有Node.js中的结构如下所示:
for (i = 0; i < 50; i++) { //Doing a for loop. } function after_forloop() { //Doing a function. } after_forloop();
那么在forloop完成之后,我怎么才能确保after_forloop()函数被触发?
如果你想看看我在做什么:
var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(1337, '127.0.0.1'); console.log('Server running at http://127.0.0.1:1337/'); var proxyChecker = require('proxy-checker'); var fs = require('fs'); function get_line(filename, line_no, callback) { fs.readFile(filename, function (err, data) { if (err) throw err; var lines = data.toString('utf-8').split("\n"); var firstLineBreak = data.toString('utf-8').indexOf("\n"); var originalText = data.toString('utf-8'); var newText = originalText.substr(firstLineBreak + 1); if(+line_no > lines.length){ return callback('File end reached without finding line', null); } callback(null, lines[+line_no], newText); }); } for (i = 0; i < 50; i++) { get_line('proxy_full.txt', i, function(err, line, newText){ fs.appendFile('proxy.txt', line + '\n', function (err) { if (err) throw err; }); fs.writeFile('proxy_full.txt', newText, function (err) { if (err) throw err; }); }) } after_forloop(); function after_forloop() { proxyChecker.checkProxiesFromFile( // The path to the file containing proxies 'proxy.txt', { // the complete URL to check the proxy url: 'http://google.com', // an optional regex to check for the presence of some text on the page regex: /.*/ }, // Callback function to be called after the check function(host, port, ok, statusCode, err) { if (ok) { console.log(host + ':' + port); fs.appendFile('WorkingProxy.txt', host + ':' + port + '\n', function (err) { if (err) throw err; }); } } ); setTimeout(function(){ fs.writeFile('proxy.txt', "", function (err) { if (err) throw err; }); console.log('Proxy Check Completed.') process.exit(1); }, 5000); }
基本上我喜欢允许节点服务器在一个列表代理服务器上运行50次testing(在五秒内)。 然后服务器应该将工作代理保存到一个新的文件。
也许这有助于:
var operationsCompleted = 0; function operation() { ++operationsCompleted; if (operationsCompleted === 100) after_forloop(); } for (var i = 0; i < 50; i++) { get_line('proxy_full.txt', i, function(err, line, newText){ fs.appendFile('proxy.txt', line + '\n', function (err) { if (err) throw err; operation(); }); fs.writeFile('proxy_full.txt', newText, function (err) { if (err) throw err; operation(); }); }) }
诚然,这不是一个优雅的解决scheme。 如果你做了很多这样的事情,你可能想看看像Async.js这样的东西 。
如果您在for循环中执行任何asynchronous操作,则可以简单地保留它
function after_forloop() { // task you want to do after for loop finishes it's execution } for (i = 0; i < 50; i++) { //Doing a for loop. if(i == 49) { after_forloop() // call the related function } }
仍然在node.js中,而不是使用具有asynchronous函数的for循环,而应该看到asynchronous模块。 这里是Async.js,或者你也可以考虑使用recursion。
如果没有魔法发生,那应该是直接的前言。
function:-
function after_forloop() { //Doing a function. }
For Loop: –
for (i = 0; i < 50; i++) { //Doing a for loop. } for (i = 0; i < 50; i++) { //Doing another for loop. } after_forloop();
这将在两个for
循环结束之后调用after_forloop
。 因为for
循环是一个阻塞调用, after_forloop()
调用必须等待。
注意:如果你在for
循环中执行一些async
任务,那么这个函数将在循环完成后被调用,但是你在循环中所做的工作可能在调用函数的时候并没有完成。