Skip to content Skip to sidebar Skip to footer

Nodejs Websockets Client Pending Promise

Trying to capture all the data from a websocket connection as a nodejs client using a promise. Script is exiting before all the messages are received and console.logging 'Promise {

Solution 1:

For it to log response #1 | goodbye you will need to wait for it to resolve.

results.then(data =>console.log(data));

Solution 2:

At the moment you are printing results it is just a promise object in pending status waiting for resolve, you will not get the result at that moment, it was just recently created. To wait for the resolve you need to execute 'then' method of the promise:

results.then(myResponse => {
 console.log(myResponse);
 // do more...
});

I recommend this talk to understand the async behavior on node.js https://www.youtube.com/watch?v=8aGhZQkoFbQ

Post a Comment for "Nodejs Websockets Client Pending Promise"