2013-08-10 2 views
0

npmjs > jsdom에서 예제 코드를 가져 왔습니다. 이 프로세스는 몇 초 안에 수행되며 그 후에 만 ​​console.log과 같은 두 번째 작업을 실행하려고합니다. 그러나 jsdom의 본문에 코드를 삽입하지 마십시오. 어쩌면 그것은 Node.js> Stream 과 작동합니다. 기능 체인을 만들면 이전 프로세스가 끝나 자마자 다음 프로세스가 시작됩니다.Node.js의 작업 순서

여기서 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"); 

답변

2

찾고 계시는 분은 Async.js입니다.

구체적으로 말하면 사용자는 series() 기능 (Run an array of functions in series, each one running once the previous function has completed)을 찾고 있습니다.

코드 예제 (이를 기반으로이야 문서) :

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"); 
});