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 { <pending> }'
, I expected it to log 'response #1 | goodbye'.
const WebSocket = require('ws');
const ws = new WebSocket('wss:url');
let results = new Promise(function(resolve, reject) {
ws.on('open', function open() {
ws.send('hello');
});
let receivedResults = '';
ws.on('message', function incoming(data) {
if (data == 'goodbye') {
receivedResults += ' | ' + data;
resolve(result);
} else {
receivedResults = data;
}
});
});
console.log(results);