我正在研究一个nodejs聊天的示例项目,我无法真正理解当callback(false)
和callback(true)
在这里被调用时会发生什么......
io.sockets.on('connection', function(socket){
socket.on('new user', function(data, callback){
if(usernames.indexOf(data) != -1){
callback(false);
} else {
callback(true);
socket.username = data;
usernames.push(socket.username);
updateUsernames();
}
});
最佳答案
回调是确认函数
服务器
socket.on('new user',
function(data, calback){
// incidentally(not needed in this case) send back data value true
calback(true);
}
);
客户端
socket.emit('new user',
data,
function(confirmation){
console.log(confirmation);
//value of confirmation == true if you call callback(true)
//value of confirmation == false if you call callback(false)
}
);
关于javascript - 回调(假)和回调(真)有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35244029/