K20GH
K20GH

Reputation: 6281

Callback is not a function?

I know this has been asked hundreds of times, however I've written loads of callback functions before and I think I've just gone a bit blind to my problem/

I have a function:

function firstSend(){
    client.write(Buffer.from([0x5C,0x57,0x31,0x32,0x33,0x34,0x2F]));

    check(function(msg){
        if(msg == true){
            console.log("Lets go");
        }
    });
}

That calls the function check with a callback

The check function returns true when it is complete:

function check(callback) {
    let m;
    if(message != null) m = message.trim();

    if(m != "OK"){
        setTimeout(check, 1000);
        return;
    }
    return callback(true);
}

Everything works correctly until it tries to do a callback, at which point it tells me its not a function.

I've logged callback out and it logs as a function, so i'm a little stumped

Upvotes: 0

Views: 87

Answers (1)

Satpal
Satpal

Reputation: 133453

You are not passing the callback in setTimeout

setTimeout(function () {
    check(callback)
}, 1000);

instead of

setTimeout(check, 1000);

OR, Alternatively you can use bind()

setTimeout(check.bind(null, callback), 1000);.

Upvotes: 6

Related Questions