gpt4 book ai didi

javascript - Nodejs websockets 客户端待处理 promise

转载 作者:太空宇宙 更新时间:2023-11-04 01:31:29 26 4
gpt4 key购买 nike

尝试使用 Promise 作为 Node.js 客户端从 WebSocket 连接捕获所有数据。在收到所有消息和 console.logging 'Promise { <pending> }' 之前脚本已退出,我预计它会记录“响应#1 |再见”。

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);

最佳答案

让它记录响应#1 |再见,您需要等待它解决。

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

关于javascript - Nodejs websockets 客户端待处理 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55983890/

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