"ble_uart" : Unhandled promise rejection

Posted on
  • used code:

    bleSendJS = function(_id, _cmd) {
        console.log('s');
        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(f­unction() {
                setTimeout(function() {
                    uart.disconnect();
                    console.log('output:',data);
                    console.log("Disconnected");
                }, 2000);
            });
        }).catch(function(e) {
            console.log("ERROR:", e);
        });
    };
    

    output

    >bleSendJS(id, nl1);
    =undefined
    BluetoothDevice: {
      "id": "d5:c7:e2:67:ac:f0 random",
      "rssi": -81,
      "data": new Uint8Array([2, 1, 6, 12, 9, 73, 79, 50, 52, 52, 55, 32, 97, 99, 102, 48]).buffer
     }
    ERROR: Disconnected
    Uncaught Error: Unhandled promise rejection: Error: Unhandled promise rejection: InternalError: BLE task completed that wasn't scheduled (4/0)
    >
    

    Is there a way to avoid that?

  • 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_Bl­uetoothRemoteGATTServer_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

  • uart.write( returns a promise, and you're not handling the rejection of that - so that might be the Unhandled promise rejection you're not managing to handle?

  • Ok, so like this?

            uart.write(`\x03\x10(${_cmd})\n`).then(f­unction() {
                setTimeout(function() {
                    uart.disconnect();
                    console.log('output:',data);
                    console.log("Disconnected");
                }, 2000);
            }).catch(function () {
                console.log("Promise Rejected - disconnect");
                uart.diconnect();
            });
    
  • Yes, that should help

  • 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(f­unction() {
                    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);
        }
    };
    
  • 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. . . .

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

"ble_uart" : Unhandled promise rejection

Posted by Avatar for MaBe @MaBe

Actions