Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
fluent API的Curried JavaScript函数_Javascript_Fluent_Currying - Fatal编程技术网

fluent API的Curried JavaScript函数

fluent API的Curried JavaScript函数,javascript,fluent,currying,Javascript,Fluent,Currying,我想为使用发出的HTTP请求编写一个默认回调。所有调用都是使用框架进行的,并且总体结果是一起处理的。如果发生错误,回调应该处理HTTP请求的结果并返回默认值。可以指定默认值,但如果未设置,则将使用null 我想使用如下流畅的语法构造我的处理程序: 句柄(完成)。withDefaultValue([])(空数组设置为默认值) 句柄(完成)(默认值为null) 我对函数咖喱比较陌生。以下是我尝试过的: 我创建了一个节点模块,最终应该像这样使用: 我在handle.js中的代码 module.expo

我想为使用发出的HTTP请求编写一个默认回调。所有调用都是使用框架进行的,并且总体结果是一起处理的。如果发生错误,回调应该处理HTTP请求的结果并返回默认值。可以指定默认值,但如果未设置,则将使用
null

我想使用如下流畅的语法构造我的处理程序:

句柄(完成)。withDefaultValue([])
(空数组设置为默认值)

句柄(完成)
(默认值为null)

我对函数咖喱比较陌生。以下是我尝试过的: 我创建了一个节点模块,最终应该像这样使用:

我在
handle.js中的代码

module.exports = function(done){
  this.withDefaultValue = function(defaultValue){
    return function(err, result){
      if(err){
        debug('handling error ' + err + ' and returning default value ' + defaultValue)
        return done(null, defaultValue)
      }
      // sanity check for null and empty objects
      result = _.isEmpty(result)?[]:result
      done(null, result)
    }
  }
  return this
}
var handle = require('handle')
async.parallel([
   function(done){
     api.myApiCall(arg1, arg2, handle(done).withDefaultValue([]))
   },
   function(done){
     api.myOtherApiCall(arg1, arg2, handle(done))
   }
], function(err, result){
})
somefile.js中的我的代码

module.exports = function(done){
  this.withDefaultValue = function(defaultValue){
    return function(err, result){
      if(err){
        debug('handling error ' + err + ' and returning default value ' + defaultValue)
        return done(null, defaultValue)
      }
      // sanity check for null and empty objects
      result = _.isEmpty(result)?[]:result
      done(null, result)
    }
  }
  return this
}
var handle = require('handle')
async.parallel([
   function(done){
     api.myApiCall(arg1, arg2, handle(done).withDefaultValue([]))
   },
   function(done){
     api.myOtherApiCall(arg1, arg2, handle(done))
   }
], function(err, result){
})
上述代码适用于第一次调用(使用
和defaultvalue([])
的调用,但不适用于第二次调用:

未处理的错误:句柄(…)。withDefaultValue不是函数


我做错了什么?

这似乎起到了关键作用:

console.log=x=>document.write(x+“
”); 函数句柄(func){ 变量处理程序=函数(参数){ log('doing stuff…'); func(param); }; var ret=handler.bind(这是“默认值”); ret.withDefault=函数(val){ 返回处理程序.bind(this,val); } 返回ret; } 函数完成(参数){ console.log(参数) } setTimeout(句柄(完成));
setTimeout(handle(done).withDefault(123));
当你说它对第二次调用有效,但对第一次调用无效时,你的意思是
async.parallel
中的第二个函数有效,但对使用
widthDefaultValue
的函数无效?对不起,我写错了。它对第一次调用有效(使用
withDefaultValue
的函数)但不是第二次调用。我现在在上面的描述中更正了这一点。这绝对与咖喱无关。你的意思是使用术语“方法链接”吗?是的,方法更改在这里可能是正确的术语,但我认为我可以通过在这里使用curry来实现这一点,因为我在一种情况下部分执行一个函数,在第二种情况下使用默认值来curry它…这似乎与我描述的不一样(至少不完全相同)。如果我理解正确,您的
句柄(func)
映射到我的
模块。导出
使用
param
作为我的
done
-函数,
处理程序
到我的代码中的
函数(err,result)
。这似乎有效,但只是部分有效,因为我无法访问err(由superagent设置):module.exports=函数(done){var handler=函数(err,result){return done(err,result)}var ret=handler.bind(this,null,{})ret.withDefaultValue=function(val){return handler.bind(this,null,val)}return ret}