0

I have predefined function, which supports callback to the second argument. I can't modify this function. How to pass callback function, so it will execute callback function with my argument and result it has received?

// predefined function, can't modify it ->>
function getEmail(uID, fn) {
    var result = { status: 'OK', data: { email: '[email protected]'} }; // answer for uID=13
    fn(result);
}
// <<- predefined function, can't modify it

var helper = {

    send: (uID, cb_function) => {
        getEmail( uID, helper.receive/*?[cb_function]?*/ ); // how to pass to the second argument cb_function?
    },

    receive: (/*?[result, cb_function]?*/) => {
        if ( result.status == 'OK' ) {
            cb_function(result.data);
        } else {
            alert('GetEmail result wasn\'t OK!');
        }
    },

}

helper.send(13, (a) => {
    // cb_function:
    alert( 'User email: ' + a.email );
});

1 Answer 1

1

Usually, you use a wrapper function:

send: (uID, cb_function) => {
    getEmail( uID, (result) => {
        helper.receive(result, cb_function);
    });
},
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.