D
D
Denisca Gareev2020-08-02 11:41:05
JavaScript
Denisca Gareev, 2020-08-02 11:41:05

How to make a function wait for a response from a WebSocket server?

There is a function that should send a websocket request to the server and receive a response in 10-15 seconds

function getData(){
var sock = io("https://server.com");
var out = "";
sock.on("mess", function(data){out = data.message});
sock.emit("send", {text: "Hello, world!"});
return out;
}

but the function ends earlier and I don't need it.
How to make a function wait for a response from websocket.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene, 2020-08-02
@Nc_Soft

You are doing something wrong. Here is asynchronous programming, you connect to a websocket, send a signal to perform an action and that's it, then just catch the signal (which will be sent by the first signal handler) that the action is completed.

D
Denioo, 2020-08-02
@Denioo

Async, await or Promise if you like the old version.
https://learn.javascript.ru/async-await
https://learn.javascript.ru/promise-basics

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question