javascript - Bind variables to callback function -


i have few functions in controller call database functions. of these database functions "error callbacks", meaning if database error occur separate callback handles error. example:

exports.referralcomplete = function(req, res){     /*getting id etc.*/     db.startdatabaseconnection(function() {         db.flagreferralasdone(id, function(success) {             db.enddatabaseconnection();             /*doing stuff on success*/         }, onerror);     }, onerror);      function onerror(err, description) {         logger.error(description + ": " + err);         user.pagenotfound(req, res);     } } 

i have multiple functions similar this, calling different database functions. problem @ moment have duplicated onerror() scope of each of them, since need req , res variables when handling error. of course pass res , req database function, , pass them arguments error callback, think there might better way.

so question is: possible somehow bind res , req global onerror callback function, in way won't have pass variables arguments db function?

i'm new node.js , javascript in general, if there better way of handling errors, please let me know.

binding simple!

db.startdatabaseconnection(function(){   // whatever }, onerror.bind(this, var1, var2)); 

you can learn more binding clicking awesome link, though link sort of long.

here's real basic demo

// function var = function (a, b, c) {   console.log(a, b, c); };  // binding of 3 defined args var b = something.bind(null, 1, 2, 3);  // call b b(); //=> 1 2 3 

behind scenes, what's happening

// es6 const mybind = (f, context, ...x) =>   (...y) => f.call(context, ...x, ...y);  // es5 var mybind = function(fn, context) {   var x = [].slice.call(arguments, 2);   return function() {     var y = [].slice.call(arguments, 0);      return fn.apply(context, x.concat(y));   }; };  var b = mybind(console.log, console, 1, 2, 3);  b(); // => 1 2 3  b(4,5,6) // => 1 2 3 4 5 6  

partial application

similar binding partial application

in computer science, partial application (or partial function application) refers process of fixing number of arguments function, producing function of smaller arity.

here make simple partial helper procedure helps accomplish this

const partial = (f, ...x) => (...y) => f(...x, ...y)    const foo = (...args) => console.log(args)    partial (foo, 1, 2, 3) (4, 5, 6)  // [ 1, 2, 3, 4, 5, 6 ]


currying

currying related to, not same as, binding or partial application

in mathematics , computer science, currying technique of translating evaluation of function takes multiple arguments (or tuple of arguments) evaluating sequence of functions, each single argument.

const curry = f => {    let aux = (n, k) =>      n === 0 ? k ([]) : x => aux (n - 1, xs => k ([x, ...xs]))    return aux (f.length, xs => f (...xs))  }    const foo = (a, b, c) => console.log(a, b, c)    curry (foo) (1) (2) (3)  // 1 2 3


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -