2014-01-09 63 views
0

讓我解釋一下我用一個簡單的例子的意思是: 我可以在關閉死亡/垃圾收集之前執行功能嗎?

(function(){ 

    db.queryRows(function(row) 
    { 
     // some sophisticated async calls could present here 
     request.write(row) 
    }) 
    require('some-cool-module').onClosureDeath(function() 
    { 
     request.end('No more data for you.') 
    }) 

})() 

難道使用域或一些其他的事情要做?如果不是,這在理論上是可能的嗎?

看來,我做了一個壞的榜樣,所以我寫了另一個問題:

function getRandomInt(min, max) 
{ 
    return Math.floor(Math.random() * (max - min + 1)) + min; 
} 

var server = require('http').createServer(function(request, response) 
{ 
    var atEnd = function() 
    { 
     response.end('No more randoms for you.') 
    }; 

    (function(){  

     response.statusCode = 200; 
     response.setHeader('Content-Type','text/html; charset=UTF-8'); 

     // This loop is emulating a huge sophisticated complex async 
     // callback delirium which cannot be easily understood and 
     // tracked down, and I won't get paid for its refactoring. 
     for(var i = 0;i < getRandomInt(0,getRandomInt(0,1000)); i++) 
     { 
      setTimeout(function() 
      { 
       response.write(String(getRandomInt(0,1000) + "<br/>")) 
      },getRandomInt(0,30000)) 
     } 

    // What I'd wish to do: 
    // require('some-cool-module').onClosureDeath(atEnd) 

    })() 
} 
).listen(11111); 
console.log("Running @ http://127.0.0.1:11111") 
+0

在db中是否有'on''end'...?在嘗試選擇模塊之前,我會先看看它。另外,你可以在你的函數中放一個回調函數,這可能更清潔 – DrakaSAN

+0

我現在面臨的問題等於db中沒有'結束'事件。我實際上不知道何時何地會結束。但在封場結束時它肯定會出現。我想以某種方式使用域,但它只有'錯誤'事件。 – user619271

+0

你使用什麼db庫?此外,嘗試回調,至少你將能夠趕上所有請求已發送 – DrakaSAN

回答

1

(不是一個真正的答案,我只是不喜歡在註釋代碼)

我唐噸覺得有什麼像這將是回調麪條

function (callback) { 
    function atEnd() { 
     console.log('No more data'); 
     callback(); 
    } 

    db.queryRows(function(row) 
    { 
     // some sophisticated async calls could present here 
     request.write(row) 
     atEnd(); 
    }) 
} 

EDIT1:(!未經測試) 我想你可以使用類似的東西

function callback_hell(callback) //Your callback hell function 
{ 
    setTimeout(function() 
    { 
     response.write(String(getRandomInt(0,1000) + "<br/>")); 
     callback(); 
    },getRandomInt(0,30000)) 
} 

function do_all(n, func, callback) { //This is just a wrapper for follow in fact 
    var i = 0; //Counter of how many instance of follow have been started 

    function follow() { //Wrapper of your callback hell (can be almost anything in fact, I use similar code for downloading files before using them 
     if (i < n) { //If there is other instance to run 
       callback_hell(follow);//Run it 
       i = i + 1; //Increment the counter 
     } else { //If all have been made 
      console.log('end'); 
      callback(); 
     } 
    } 

    //What must be made before the loop 
    response.statusCode = 200; 
    response.setHeader('Content-Type','text/html; charset=UTF-8'); 
    //Start the loop 
    follow(); 
} 

do_all(getRandomInt(0, getRandomInt(0, 1000)), callback_hell, function() { 
    request.end('No more data for you.'); 
    console.log('done?'); 
}); 
+0

你會考慮我剛添加的另一個嗎? – user619271

+0

我現在正在看它 – DrakaSAN

+0

您可能可以使用類似的東西,我沒有測試過,因爲我手邊沒有node.js,所以告訴我它是否有錯誤。 – DrakaSAN