处理异步非阻塞模型的麻烦

时间:2014-09-25 17:22:30

标签: node.js asynchronous socket.io

我无法绕过节点的异步模型,我有这个功能:

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变量设置了值。

2 个答案:

答案 0 :(得分:0)

您需要将回调传递给testAvailability,然后在socket的{​​{1}}事件中调用它:

available

然后更改您的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

有几点需要注意:

  1. function getstream() { console.log('calling testAvail, Avail value is:' + available); testAvailability(function() { console.log('Available:'+available); if (available || SelfSharing) { // Do something setDefaults(); } }); } 现在是异步的。如果有其他东西在等待它完成,你需要更改它以便它也需要一个回调函数。
  2. 我发现您在getstream的{​​{1}}事件中正在进行某种重试。在这种情况下可能没问题,这取决于你的应用程序,但通常情况下,你会希望在发生错误时将错误传回作为回调的第一个参数。

答案 1 :(得分: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);
    });
}