简体   繁体   English

如何将功能绑定到Async.js

[英]How to bind function to Async.js

I have the following block of code that gets called via an internal API. 我有以下通过内部API调用的代码块。
I'm trying to use async.js to do some flow control here but as you'll see below we lose context to "R". 我试图在这里使用async.js进行一些流控制,但是正如您将在下面看到的,我们将上下文丢失为“ R”。 I also lose content to "this" which is addressed with the ugly but simplest solution of "self = this". 我也对“这个”感到迷惑,这用“自我=这个”的丑陋但最简单的解决方法解决了。

So my question is how do i bind R to the async.series? 所以我的问题是如何将R绑定到async.series? I'm syntactically confused on how to do this. 我在语法上对如何执行此操作感到困惑。

"condition": function(R) {
    var self = this;
    async.series([
        function(R){
            R.check(List.redisTables.List.negEmail, self.customer.email)
        },
        function(R) {
            R.when(self.virtual.negEmail === "true")
        }
    ])

}, 
 ///more code 

Here is an easier to test similar example: 这是一个更容易测试的类似示例:

  function getFunctions(asd) { var arr = []; arr.push(function(asd) { console.log(asd) }.bind(this, asd)); return arr }; var funcs = getFunctions("yay"); for (var i = 0; i < funcs.length; i++) { funcs[i]() } 

So basically, you need to bind the R to the function as argument. 因此,基本上,您需要将R绑定到函数作为参数。 In your case, it would be: 在您的情况下,它将是:

async.series([
    function(R){
        R.check(List.redisTables.List.negEmail, this.customer.email)
    }.bind(this,R),
    function(R) {
        R.when(this.virtual.negEmail === "true")
    }.bind(this,R)
])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM