[英]how does the callback in fs.readfile get called when using fs.readfile.bind(context,pathArgument)
How does the callback in fs.readfile get called when using fs.readfile.bind(context,pathArgument) like so. 像这样使用fs.readfile.bind(context,pathArgument)时,如何调用fs.readfile中的回调。 //understandable because my task function knows the name of the callback parameter
//这是可以理解的,因为我的任务函数知道回调参数的名称
async.series([function(callback){
//operation done callback()},...],finalCallback(err,result));
BUT //not understandable 但是//无法理解
async.series([fs.someOperation.bind(null,firstArgument),...],finalCallback(err,esult))
I believe I understand partial application;however, it would look something like this. 我相信我了解部分应用程序;但是,它看起来像这样。
function(callback){ fs.someOperation(firstArgument, ????)}(asyncCallbackFunc)
and then I have no idea how the second argument is called... Thx, in advance for helping me clear this up. function(callback){ fs.someOperation(firstArgument, ????)}(asyncCallbackFunc)
,然后我不知道第二个参数的调用方式……Thx,是为了帮助我提前解决这个问题。
All bind
does is set the context of the callback. 所有
bind
操作都设置了回调的上下文。 It is still a regular callback like any other. 像其他任何回调一样,它仍然是常规回调。 Except, it is explicitly told what
this
will be. 除非明确告知
this
将是什么。 Looks like in your case, it is set to null
. 在您的情况下,它设置为
null
。
The bind
function on function object allows you to set the context ie the value of this
inside the function body as well as allow you to create a partial function in case you pass some arguments while calling bind
. 函数对象上的
bind
函数允许您设置上下文,即函数体内的this
值,并允许您在调用bind
传递一些参数的情况下创建局部函数。
For example: 例如:
function add(a,b) {
console.log(this);
return a+b;
}
var newAdd = add.bind("hello world", 10);
The newAdd
will be one argument function which gets added to 10 and the result is returned. newAdd
将是一个参数函数,该函数将被添加到10中并返回结果。 Also when newAdd is called the "hello world" will be logged in console. 同样,在调用newAdd时,“ hello world”也会登录到控制台。
Now when your code says fs.readFile.bind(null, path)
it means that the return function will be of one argument which is the callback for readfile ie the return function will be of form function(callback) { ... }
which is exactly what is required to be passed to async.series
现在,当您的代码显示
fs.readFile.bind(null, path)
这意味着返回函数将具有一个参数,即readfile的回调,即返回函数将具有function(callback) { ... }
形式正是需要传递给async.series
The main idea in the code you posted is to create a partial function that accepts only callback so that it can be passed to async.series
the null argument doesn't play any role but you need to pass a context argument to call bind function, hence a null
is passed as context arg. 您发布的代码中的主要思想是创建一个仅接受回调的部分函数,以便可以将其传递给
async.series
null参数不起作用,但是您需要传递上下文参数来调用bind函数,因此,将null
作为上下文arg传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.