gpt4 book ai didi

javascript - 回调(假)和回调(真)有什么作用?

转载 作者:太空宇宙 更新时间:2023-11-04 00:44:40 25 4
gpt4 key购买 nike

我正在研究一个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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com