Aer*_*ang 5 javascript node.js
假设我在Node.js中有一个结构,如下所示:
for (i = 0; i < 50; i++) {
//Doing a for loop.
}
function after_forloop() {
//Doing a function.
}
after_forloop();
Run Code Online (Sandbox Code Playgroud)
那么,如何确保在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);
}
Run Code Online (Sandbox Code Playgroud)
基本上,我希望允许节点服务器一次(在五秒钟内)在列表代理服务器上运行50个测试。然后,服务器应将工作代理保存到新文件中。
如果没有发生任何魔法,那么它应该是直截了当的。
功能:-
function after_forloop() {
//Doing a function.
}
Run Code Online (Sandbox Code Playgroud)
For 循环:-
for (i = 0; i < 50; i++) {
//Doing a for loop.
}
for (i = 0; i < 50; i++) {
//Doing another for loop.
}
after_forloop();
Run Code Online (Sandbox Code Playgroud)
这将after_forloop在两个for循环完成后立即调用。因为forloop是一个阻塞调用,所以调用ofafter_forloop()必须等待。
注意:-如果您在循环中执行某些async任务for,则该函数将在循环完成后被调用,但您在循环中执行的工作可能在调用函数时尚未完成。
也许这会有所帮助:
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();
});
})
}
Run Code Online (Sandbox Code Playgroud)
诚然,这不是一个优雅的解决方案。如果您要执行大量操作,则可能需要检查类似Async.js的内容。
如果您在for循环中执行任何异步操作,则只需像
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
}
}
Run Code Online (Sandbox Code Playgroud)
仍然在node.js中,而不是使用带有异步函数的for循环,您应该看到async模块。这是Async.js,或者您也可以考虑使用递归。
| 归档时间: |
|
| 查看次数: |
16819 次 |
| 最近记录: |