-
• #2
Tue 2021.09.07
It appears that the error may be caused and thrown from within the disconnect() function at L15
http://www.espruino.com/Reference#t_l_BluetoothRemoteGATTServer_disconnect
function BluetoothRemoteGATTServer.disconnect()
Returns: A Promise that is resolved (or rejected) when the disconnection is complete (non-standard)See Note: 'In Espruino we return a Promise to make it easier to detect when Espruino is free to connect to something else.'
That said, I thought it would be a breeze to add detection code, but code location and the syntax is a bit different with nested functions.
Maybe this might provide some clues:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch
-
• #3
uart.write(
returns a promise, and you're not handling the rejection of that - so that might be theUnhandled promise rejection
you're not managing to handle? -
• #4
Ok, so like this?
uart.write(`\x03\x10(${_cmd})\n`).then(function() { setTimeout(function() { uart.disconnect(); console.log('output:',data); console.log("Disconnected"); }, 2000); }).catch(function () { console.log("Promise Rejected - disconnect"); uart.diconnect(); });
-
• #5
Yes, that should help
-
• #6
There is the need for a additional try catch to handle
Uncaught Error: BLE task DISCONNECT is already in progress
bleSendJS = function(_id, _cmd) { console.log('s'); try { NRF.requestDevice({ timeout: 5E3, filters: [{ id: _id }] }).then(function(device) { console.log('f'); console.log(device); return require("ble_uart").connect(device); }).then(function(uart) { console.log('c'); var data; uart.on('data', function(d) { data += d; }); uart.write(`\x03\x10(${_cmd})\n`).then(function() { setTimeout(function() { uart.disconnect(); console.log('output:', data); console.log("Disconnected"); }, 2000); }).catch(function() { console.log("Promise Rejected - disconnect"); uart.diconnect(); }); }).catch(function(e) { console.log("ERROR:", e); }); } catch (e) { console.log("ERROR:", e); } };
-
• #7
Sat 2021.09.25
Without writing any code or testing on my part;
I originally thought the try/catch would have to wrap/start at L14 as the user forced disconnect is within the setInterval(). So is that the promise reponse isn't really attached to any code as it responds, that the catch wrapper had to be around the entire code block as you have shown perhaps?
In any event, thank you for the snippet @MaBe as this will come in handy down the road. . . .
used code:
output
Is there a way to avoid that?