0

I am currently working in node.js sending a control transfer to my usb device and need to be able to detect when the return value of the request has changed. This then needs to be put into an if/else block in order to handle the value the device sends back.

I tried using a while loop expecting some kind of change, but all it did was create an infinite loop.

function checkUpdateStatus (callback) {
    device.open();
    device.controlTansfer(reqType, req, value, dataLength, function getResponse(err, data) {
        if (!err) feedback = data;
        else feedback = err;
        callback();
    })
}

checkUpdateStatus(function logFeedback() {
    console.log(feedback);
})

while (feedback !== 0 && feedback !== 1) {
    if (feedback === 1) {
        throw new Error('Install incomplete');
    } else if (feedback === 0){
        return ;
    } else {
        checkUpdateStatus(function logFeedback() {
            console.log(feedback);
        })
    }
}

Eventually, the response from the request should send either a 1 or a 0 as seen in the code above, which does happen, but the feedback variable remains the same. Is there a way to add an event listener to this variable to detect a change or would I have to just modify the while loop.

Any help is greatly appreciated.

Please note, I understand this code would not work correctly for what I am trying to achieve, but this is just the format of what I tried previously.

0

Browse other questions tagged or ask your own question.