在Node.js的操作顺序 [英] Sequence of operation in Node.js
问题描述
我接过例如code从 npmjs> jsdom 。在几秒钟内执行这个过程,只有经过它,我想运行第二个动作,如的console.log 。但是,不要在jsdom体内插入code。也许这是工作,的Node.js>流
Whant创造的功能链,下道工序为尽快启动previous的结束。
在那里我可以在Node.js的阅读顺序?
VAR jsdom =要求(jsdom);jsdom.env({
网址:http://news.ycombinator.com/
脚本:[HTTP://$c$c.jquery.com/jquery.js],
完成:功能(错误,窗口){
。变量$ =窗口$;
的console.log(HN链接);
$(td.title:没有(:最后一台))。每个(函数(){
的console.log( - ,$(本)的.text());
});
}
});的console.log(最终);
您正在寻找 Async.js
。
要具体,你要找的 系列()
功能(运行串联功能的阵列,每一个运行一次previous功能已完成
)。
code例子(基于它的文档):
async.series([
函数(回调){
jsdom.env({
网址:http://news.ycombinator.com/
脚本:[HTTP://$c$c.jquery.com/jquery.js],
完成:功能(错误,窗口){
。变量$ =窗口$;
的console.log(HN链接);
$(td.title:没有(:最后一台))。每个(函数(){
的console.log( - ,$(本)的.text());
});
回调(空,'一');
}
});
},
函数(回调){
//做一些更多的东西(第二个任务)...
回调(NULL,两节);
}
]
//可选的回调
功能(犯错,结果){
的console.log(最终);
});
I took the example code from npmjs > jsdom. This process is performed in a few seconds and only after it I want to run a second action, such as console.log. But do not insert the code in the body of jsdom. Maybe it's work with Node.js > Stream Whant to create a chain of functions, next process start as soon as the end of the previous.
Where i can read about sequence in Node.js?
var jsdom = require("jsdom");
jsdom.env({
url: "http://news.ycombinator.com/",
scripts: ["http://code.jquery.com/jquery.js"],
done: function (errors, window) {
var $ = window.$;
console.log("HN Links");
$("td.title:not(:last) a").each(function() {
console.log(" -", $(this).text());
});
}
});
console.log("The end");
You're looking for Async.js
.
To be specific, you're looking for its series()
functionality (Run an array of functions in series, each one running once the previous function has completed
).
Code example (based on it's docs):
async.series([
function(callback){
jsdom.env({
url: "http://news.ycombinator.com/",
scripts: ["http://code.jquery.com/jquery.js"],
done: function (errors, window) {
var $ = window.$;
console.log("HN Links");
$("td.title:not(:last) a").each(function() {
console.log(" -", $(this).text());
});
callback(null, 'one');
}
});
},
function(callback){
// do some more stuff (second task) ...
callback(null, 'two');
}
],
// optional callback
function(err, results){
console.log("The end");
});
这篇关于在Node.js的操作顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!