javascript socket.io - socket.on 等待承诺
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19608923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
socket.io - socket.on wait for promise
提问by Tamas
I have a button that does some communication with the server to check if an entered value (via an input box) already exists. The code is the following:
我有一个按钮与服务器进行一些通信,以检查输入的值(通过输入框)是否已经存在。代码如下:
$("#button").click(function () {
var exists = false;
var name = $("#name").val();
socket.emit("check", name);
socket.on("checkReturn", function (data) {
exists = data.result;
});
if (exists) {
console.log("exists")
} else {
if (name.length > 0) {
socket.emit("create", name);
}
}
});
});
The problem is that the checkReturn
call is asynchronous, and therefore the code carries on without actually waiting for the result. How do I make sure that checkReturn
is first finished and only then the rest of the code gets executed?
问题在于checkReturn
调用是异步的,因此代码会继续执行而无需实际等待结果。我如何确保checkReturn
首先完成,然后才执行其余的代码?
回答by hexacyanide
Aside from the other answer, you can also use acknowledgements, where a callback is passed between the client and server. Then you can just use the callback of the emit function:
除了其他答案,您还可以使用确认,在客户端和服务器之间传递回调。然后你可以只使用emit函数的回调:
$("#button").click(function() {
var exists = false;
var name = $("#name").val();
socket.emit('check', name, function (data) {
exists = data.result;
if (exists) console.log("exists");
else (if (name.length > 0) socket.emit("create", name));
});
});
On the server side it would look like this:
在服务器端,它看起来像这样:
io.sockets.on('connection', function (socket) {
socket.on('ferret', function(name, fn) {
// find if "name" exists
fn({ exists: false });
});
});
回答by Vicky Gonsalves
$("#button").click(function ()
{ var exists = false;
var name = $("#name").val();
socket.emit("check", name);
socket.on("checkReturn", function (data) {
exists = data.result;
if (exists) { console.log("exists") } else { if (name.length > 0) { socket.emit("create", name); } } });
});
回答by pvsfair
An alternative for @hexacyanide aswer could be done like that:
@hexacyanide aswer 的替代方案可以这样完成:
$("#button").click(async function() {
var exists = false;
var name = $("#name").val();
exists = await new Promise(resolve => socket.emit('check', name, data => resolve(data.result)))
if (exists) console.log("exists");
else (if (name.length > 0) socket.emit("create", name));
});
The socket emit is wrapped inside a promisse so it can wait for the callback, and avoid the nesting inside multiple curly braces.
套接字发射被包裹在一个承诺中,因此它可以等待回调,并避免嵌套在多个花括号内。
Is not that elegant, but could make your code easier to read if it is encapsulated inside a separated function:
不是那么优雅,但如果将代码封装在单独的函数中,可以使您的代码更易于阅读:
function check(name){
return new Promise(resolve => socket.emit('check', name, data => resolve(data.result)))
}
And used like such:
并像这样使用:
$("#button").click(async function() {
var exists = false;
var name = $("#name").val();
exists = await check(name)
if (exists) console.log("exists");
else (if (name.length > 0) socket.emit("create", name));
});