Exitos
Exitos

Reputation: 29720

How Do I use .bind in Node.JS?

Still struggling to get my head around .bind.

If I have...

 dbAccess.runCommand('INSERT INTO users (email) values (?); SELECT LAST_INSERT_ID()',
                            req.getConnection,
                            email)()
                .then(setProjectPermission(req, res, 'insertId', projectId, permissionLevel));

and....

function setProjectPermission(req, res, arg, projectId, permissionLevel) {

  return function(result) {

    var id = result.rows[0][arg];

    ...

Can I use .bind so I don't have to use a function which returns a function? I'm struggling to know how to make the anonymous closure. For example if it was EC6 I would do this (I think)...

 dbAccess.runCommand('INSERT INTO users (email) values (?); SELECT LAST_INSERT_ID()',
                            req.getConnection,
                            email)()
                .then(result => setProjectPermission(req, res, result.insertId, projectId, permissionLevel));

and....

function setProjectPermission(req, res, idArg, projectId, permissionLevel) {

    var id = idArg;

    ...

Is this possible to do without returning the function which is expecting 'result' ?

Upvotes: 2

Views: 180

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1075039

For example if it was EC6 I would do this (I think)...

("EC6" => "ES2015" aka "ES6") Yes, exactly, and all up-to-date versions of Node support arrow function syntax. Except that your () at the end are a bit dodgy:

dbAccess.runCommand('INSERT INTO users (email) values (?); SELECT LAST_INSERT_ID()',
                        req.getConnection,
                        email)()
// ---------------------------^^ These shouldn't be there, you've *already* called `runCommand`
            .then(result => setProjectPermission(req, res, result.insertId, projectId, permissionLevel));

So (apologies, I've also adjusted indentation):

dbAccess.runCommand(
        'INSERT INTO users (email) values (?); SELECT LAST_INSERT_ID()',
        req.getConnection,
        email
    )
    .then(result => setProjectPermission(req, res, result.insertId, projectId, permissionLevel));

If you need to run on legacy versions of Node, you can use function syntax:

dbAccess.runCommand(
        'INSERT INTO users (email) values (?); SELECT LAST_INSERT_ID()',
        req.getConnection,
        email
    )
    .then(function result {
        return setProjectPermission(req, res, result.insertId, projectId, permissionLevel);
    });

Since you're not using this in there, no need for bind.

If you were using this in there, you'd add bind to the end:

dbAccess.runCommand(
        'INSERT INTO users (email) values (?); SELECT LAST_INSERT_ID()',
        req.getConnection,
        email
    )
    .then(function result {
        return setProjectPermission(req, res, result.insertId, projectId, permissionLevel);
    }.bind(this)); // <===== `bind` is here

...or use var _this = this; and use _this instead, or any of the tricks described in this question's answers. But with arrow functions, you don't have to worry about that, they close over this.

Upvotes: 3

Related Questions