0

I would like to know if there is a way to write the following code quicker.

items.getAll() callback has to take the same arguments as database.query() callback. So I just would like to "rethrow" the callback without changing it.

items.getAll = function(callback){
    database.query('SELECT * FROM items', function(err, rows){
        callback(err, rows);
    });
};

1 Answer 1

1

Just pass the callback directly:

    database.query('SELECT * FROM items', callback);

An even shorter way available in modern JavaScript runtimes (that is, not old IE):

items.getAll = database.query.bind(database, "SELECT * FROM items");
Sign up to request clarification or add additional context in comments.

2 Comments

@Nicolas no problem - see the addition I made to the answer.
Ok thx, as it's server side code, I should not have compatibility issues. Will read a bit about bind().

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.