Javascript 回调(false)和回调(true)的作用是什么?
我正在研究一个nodejs聊天的示例项目,我真的不明白在这里调用Javascript 回调(false)和回调(true)的作用是什么?,javascript,node.js,callback,Javascript,Node.js,Callback,我正在研究一个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); } e
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侦听事件“新用户” 此事件通过以下方式触发:
var data= "mydata"
var callback=function(bool){
if(bool){
console.log('success')
}else{
console.log('error')
}
}
socket.trigger('new_user',[data,callback])
回调只是一个函数传递,因为触发器的参数是确认函数 服务器
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)
}
);
callback
是一个作为参数传递的函数,callback(true)
,callback(false)
只是用必要的参数调用函数。谢谢!现在更好地理解这一点:)