这段代码是异步执行的吗?

时间:2014-03-01 16:16:58

标签: node.js

我正在学习Node.js,我开始理解异步函数调用的概念。但现在我有了一个新的难题。

我有一些看起来像这样的代码(你不必详细阅读它,它只是一个用来说明我的问题的摘录):

var seat_winner = states[gid].current_trick[trick_winner].seat;console.log(seat_winner+" won "+trick_pts+" points");
            states[gid][seat_winner].points += trick_pts;
            if(states[gid][seat_winner].team == "blue"){
                states[gid].points_blue += trick_pts;
                states[gid].points_blue_total += trick_pts;
            }
            else{
                states[gid].points_red += trick_pts;
                states[gid].points_red_total += trick_pts;
            }

            states[gid].current_trick = {
                1: {seat: 0, card: "N"},
                2: {seat: 0, card: "N"},
                3: {seat: 0, card: "N"},
                4: {seat: 0, card: "N"}
            };
            states[gid].trick_in_progress++;
            var score = [states[gid].points_blue, states[gid].points_red, states[gid].points_blue_total, states[gid].points_red_total, states[gid].game40holder, states[gid].trump];
            io.sockets.in(room).emit('score', score);

            //also tell the client to clean up the display for the next trick
            io.sockets.in(room).emit('cleanUp', null);

next++;
        if(next > 4)
            next -= 4;
        states[gid].next_to_play = next;
        states[gid].current_trick[order_to_play] = { "seat": seat, "card": states[gid][seat].hand[to_play] };
        states[gid].played_cards.push(states[gid][seat].hand[to_play]);
        states[gid][seat].hand[to_play] = "N";

        nextMove(gid);

我看到这里没有函数调用,除了最后的nextMove()函数。但是我在客户端上得到了一些奇怪的结果,就像上面的代码完全异步执行一样。但这没有意义,是吗?以上是否有任何理由异步执行?

1 个答案:

答案 0 :(得分:0)

我看不到上面代码中指定的任何回调函数。

代码中只为您创建问题的代码就是以下几行。

  
       io.sockets.in(room).emit('score', score);

       //also tell the client to clean up the display for the next trick
       io.sockets.in(room).emit('cleanUp', null);
  

当第一次发射时,紧接着发生第二次发射。也许这不是你想要的......也许你想在第一次发射完成时发出第二次发射!

编辑:所以你可以说上面的行是异步的,因为即使在第一个完全完成之前就会触发第二次发射。