如何从异步回调中返回true或false来打破some()循环?

时间:2015-08-14 03:39:30

标签: node.js asynchronous mongoose

user.some(function(value) {

    CreateRoomHash(userID, value._id, function (err, roomhash) {
        if(err) console.log(err);
        if(roomhash) {
        console.log("ROOMHASH:" + roomhash);
            retrieveRoom(roomhash, function(err, room) {
                if(err) console.log(err);
                if(room) {
                    console.log("ROOM FOUND: " + room);
                    if(room.status) {
                        console.log("Existing room found:"+ roomsha);
                        JoinRoom(roomsha);
                        response.json({Roomhash: roomsha});
                        return true;
                    } else {
                        console.log("ROOM STATUS IS FALSE");
                        return false;
                    }
                } else {
                    console.log("ROOM NOT FOUND");
                    var newRoom = CreateRoom(roomsha, userID, matchedID); 
                    console.log("No existing room found, new room created:" + roomsha);
                    response.json({Roomhash: newRoom});
                    return true;
                }
            });
            console.log("COOL:"+cool)
            }
            console.log("COOL:"+cool)
        });
    });
});

初学者。我有两个嵌套的回调函数,CreateRoomHash和retrive Room。如何为我的回调函数返回true或false,这样我可以打破我的some()?

0 个答案:

没有答案