2014-09-25 78 views
0
我在與周圍節點的異步模型越來越麻煩

,我有這樣的功能:故障處理異步非阻塞模式

function getstream() { 

    console.log('calling testAvail, Avail value is:' + available); 
    testAvailability(); 
    console.log('Available:'+available); 
    if (available || SelfSharing) { 

     // Do something 
     setDefaults(); 

     return; 
    } 
} 

其中要求testAvailability()函數的定義爲:

function testAvailability() 
{ 
    console.log('entered test'); 
    var stat; 
    var socket = io.connect('http://somedomain.com'); 
    socket.on('error', function() { 

     console.log('There was an error at server end please try again'); 
     // chrome.runtime.reload(); 

     setTimeout(chrome.runtime.reload(),3000); 
     //here i change options 
     //socket = io.connect(host, options); 
    }); 
    socket.emit('available'); 
    socket.on('available', function (status) { 
     console.log('got status from server which is:'+ status); 
     available=status; 
     console.log("inside the socket.on"+available); 
     console.log('leaving test, do you see any got status above?'); 
    }); 
} 

我想讓getstream()在testAvailability()完成後繼續離開它,available變量設置了它的值。

回答

0

你需要一個回調傳遞給testAvailability然後調用它socketavailable事件中:

function testAvailability(callback) 
{ 
    console.log('entered test'); 
    var stat; 
    var socket = io.connect('http://somedomain.com'); 
    socket.on('error', function() { 

     console.log('There was an error at server end please try again'); 
     // chrome.runtime.reload(); 

     setTimeout(chrome.runtime.reload(),3000); 
     //here i change options 
     //socket = io.connect(host, options); 
    }); 
    socket.emit('available'); 
    socket.on('available', function (status) { 
     console.log('got status from server which is:'+ status); 
     available=status; 
     console.log("inside the socket.on"+available); 
     console.log('leaving test, do you see any got status above?'); 

     callback(); <-- CALL YOUR CALLBACK FUNCTION HERE 
    }); 
} 

然後改變你的getstream功能,使得它傳遞一個回調testAvailability和移動功能的其餘部分該回調裏面:

function getstream() { 

    console.log('calling testAvail, Avail value is:' + available); 
    testAvailability(function() { 
     console.log('Available:'+available); 
     if (available || SelfSharing) { 

      // Do something 
      setDefaults(); 
     } 
    }); 
} 

幾件事情需要注意:

  1. getstream現在是異步的。如果有其他事情正在等待它完成,則需要對其進行更改,以便還需要回調函數。
  2. 我看到你在socketerror事件中做某種重試。在這種情況下,這可能是正確的,它取決於您的應用程序,但通常,您會希望在發生錯誤時將錯誤作爲回調的第一個參數傳回。
+0

真周到的你添加的最後2分,我會在坑我敢肯定,感謝一直下降。 – 2014-09-25 19:53:23

0

這裏,我們去:

function getstream() { 

    console.log('calling testAvail, Avail value is:' + available); 
    testAvailability(function(available){ 
     console.log('Available:'+available); 
     if (available || SelfSharing) { 

      // Do something 
      setDefaults(); 

      return; 
     } 
    }); 
} 

function testAvailability(callback) { 
    console.log('entered test'); 
    var stat; 
    var socket = io.connect('http://somedomain.com'); 
    socket.on('error', function() { 

     console.log('There was an error at server end please try again'); 
     // chrome.runtime.reload(); 

     setTimeout(chrome.runtime.reload(),3000); 
     //here i change options 
     //socket = io.connect(host, options); 

     callback(false); 
    }); 
    socket.emit('available'); 
    socket.on('available', function (status) { 
     console.log('got status from server which is:'+ status); 
     console.log('leaving test, do you see any got status above?'); 
     callback(true); 
    }); 
}