运行W3C TCPSocket示例报missing ) after argument list语法错误求解
报错根因
- 你观察到的报错触发位置完全准确:所有写在失败回调箭头函数行尾的分号,就是核心问题来源。这些分号全部处在
.then()Promise 方法的两个回调参数中间,JS解析器遇到分号就会判定当前语句结束,此时.then()的左括号还没匹配到对应的右闭合括号,直接抛出「missing ) after argument list」的语法错误。 - 连带的语法问题是权限拒绝回调里的双引号字符串被手动拆成了物理两行,JS 原生语法不允许单/双引号包裹的普通字符串跨换行,这也会加重解析报错。
- 你怀疑的未闭合引号并不是本次报错的主因。
修正说明
- 移除所有
.then()方法内部两个回调参数之间的错误分号,把分号的位置移到整个Promise调用语句的最末尾。 - 把跨物理行的错误字符串合并为单行字符串,符合JS基础语法规则。
修正后的完整代码
navigator.tcpPermission.requestPermission({remoteAddress:"127.0.0.1", remotePort:6789}).then( () => { // Permission was granted // Create a new TCP client socket and connect to remote host var mySocket = new TCPSocket("127.0.0.1", 6789); // Send data to server mySocket.writeable.write("Hello World").then( () => { // Data sent sucessfully, wait for response console.log("Data has been sent to server"); mySocket.readable.getReader().read().then( ({ value, done }) => { if (!done) { // Response received, log it: console.log("Data received from server:" + value); } // Close the TCP connection mySocket.close(); } ); }, e => console.error("Sending error: ", e) ); // Signal that we won't be writing any more and can close the write half of the connection. mySocket.halfClose(); // Log result of TCP connection attempt. mySocket.opened.then( () => { console.log("TCP connection established sucessfully"); }, e => console.error("TCP connection setup failed due to error: ", e) ); // Handle TCP connection closed, either as a result of the webapp // calling mySocket.close() or the other side closed the TCP // connection or an error causing the TCP connection to be closed. mySocket.closed.then( () => { console.log("TCP socket has been cleanly closed"); }, e => console.error("TCP socket closed due to error: ", e) ); }, e => console.error("Connection to 127.0.0.1 on port 6789 was denied due to error: ", e) );
内容的提问来源于stack exchange,提问作者Caro
相关产品推荐
相关产品推荐

