将变量绑定到回调函数

将变量绑定到回调函数

本文介绍了将变量绑定到回调函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的控制器中有一些函数调用了一些数据库函数。所有这些数据库函数都执行错误回调,这意味着如果发生数据库错误,它们会执行单独的回调来处理错误。示例:

I have a few functions in my controller which call some database functions. All of these database functions do "error callbacks", meaning that if a database error occur they do a separate callback which handles the 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);
    }
}

我有多个与此类似的函数,它们正在调用不同的数据库功能问题是我现在已经将onError()复制到每个的范围内,因为我在处理错误时需要req和res变量。我当然可以将res和req传递给数据库函数,然后将它们作为参数传递给错误回调,但我觉得可能有更好的方法。

I have multiple functions similar to this, which are calling different database functions. The problem is that I at the moment have duplicated onError() into the scope of each of them, since I need the req and res variables when handling the error. I could of course pass res and req to the database function, and then pass them as arguments to the error callback, but I like think that there might be a better way.

所以问题是:是否有可能以某种方式将res和req绑定到全局onError回调函数,以某种方式我不必将变量作为参数传递给db函数?

So the question is: Is it possible to somehow bind res and req to a global onError callback function, in a way that I won't have to pass the variables as arguments to the db function?

我对node.js和javascript很新,所以如果有更好的方法来处理错误,请告诉我。

I'm very new to node.js and javascript in general, so if there is a better way of handling errors, please let me know.

推荐答案

绑定很简单!

db.startDatabaseConnection(function(){
  // whatever
}, onError.bind(this, var1, var2));

这是一个真正的基本演示

Here's a real basic demo

// a function
var something = function (a, b, c) {
  console.log(a, b, c);
};

// a binding of something with 3 defined args
var b = something.bind(null, 1, 2, 3);

// call b
b();
//=> 1 2 3

在幕后,这基本上就是发生了什么

Behind the scenes, this is basically what's happening

// ES6
const myBind = (f, context, ...x) =>
  (...y) => f.call(context, ...x, ...y);

// ES5
var myBind = function(fn, context) {
  var x = Array.prototype.slice.call(arguments, 2);
  return function() {
    var y = Array.prototype.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






部分应用程序

类似于 bind ing是

我们可以制作一个非常简单的部分帮助程序,帮助我们完成这个

Here we could make a very simple partial helper procedure which helps us accomplish this

const identity = x =>
  x

const partial = (f = identity, ...x) =>
  (...y) => f (...x, ...y)

const foo = (...all) =>
  console.log ('array of', all)

partial (foo, 1, 2, 3) (4, 5, 6)
// 'array of', [ 1, 2, 3, 4, 5, 6 ]

Currying

是与约束或部分应用相关但不相同

Currying is related to, but not the same as, binding or partial application

const identity = x =>
  x

const curry = (f = identity, arity = f.length) => x =>
{
  const next = (xs = []) =>
    xs.length >= arity
      ? f (...xs)
      : x => next ([ ...xs, x ])
  return next ([ x ])
}

const foo = (a, b, c) =>
  console.log ('a', a, 'b', b, 'c', c)

curry (foo) (1) (2) (3)
// 'a' 1 'b' 2 'c' 3

curry (foo) ('choo') ('bye') ()
// 'a' 'choo' 'b' 'bye' 'c' undefined

这篇关于将变量绑定到回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 08:38