[英]How can I pass an argument to a callback
I am writing a ping application that logs every ping to a log file. 我正在编写一个ping应用程序,它将每个ping记录到一个日志文件中。 Depending on the result I write an info or a error entry.
根据结果,我编写了一个信息或错误条目。
Here is the ping function: 这是ping函数:
function pingServer(item){
var monitor = new Monitor({
website: item.url,
interval: interval
});
monitor.on('up', writeInfo);
monitor.on('down', writeError);
}
I would like to have a unique callback function writeLog instead of writeInfo and writeError functions, but I dont know how to pass an argument in that callback. 我想拥有一个独特的回调函数writeLog,而不是writeInfo和writeError函数,但是我不知道如何在该回调中传递参数。
So instead of 所以代替
function writeInfo(res){
logger.info(getVarLine(res));
}
function writeError(res){
logger.error(getVarLine(res));
}
I would have something like: 我会有类似的东西:
function writeLog(res, level){
if (level=='info'){
logger.info(getVarLine(res));
} else {
logger.error(getVarLine(res));
}
}
Here are three options: 这是三个选项:
.bind()
to automatically prepend arguments to the callback (technically this creates a new function stub that adds that argument). .bind()
自动将参数添加到回调之前(从技术上讲,这将创建一个添加该参数的新函数存根)。 .on()
that accepts your level
argument and adds it to the callback before calling the callback. .on()
,它接受您的level
参数并将其添加到回调中,然后再调用该回调。 .bind()
.bind()
If you are not relying on a value of this
being passed into your callback, then you can use .bind()
to create a new function that will have the level
argument automatically prepended to the callback arguments. 如果不依靠值
this
被传递到你的回调,那么你可以使用.bind()
来创建将有一个新的功能level
自动前置到回调的论点论据。
// note how the arguments had to be switched here so level was first to match
// up with how .bind() works
function writeLog(level, res){
if (level=='info'){
logger.info(getVarLine(res));
} else {
logger.error(getVarLine(res));
}
}
function pingServer(item){
var monitor = new Monitor({
website: item.url,
interval: interval
});
monitor.on('up', writeLog.bind(null, level));
monitor.on('down', writeLog.bind(null, level));
}
Or, you can create your own wrapper function that returns a function, but captures the parameter you wanted: 或者,您可以创建自己的包装器函数,该包装器函数返回一个函数,但捕获所需的参数:
function writeLogCallback(level) {
return function(res) {
if (level=='info'){
logger.info(getVarLine(res));
} else {
logger.error(getVarLine(res));
}
}
}
function pingServer(item){
var monitor = new Monitor({
website: item.url,
interval: interval
});
monitor.on('up', writeLogCallback(level));
monitor.on('down', writeLogCallback(level));
}
.on()
.on()
创建替换 Or, you can create a wrapper for monitor.on()
that captures your level value: 或者,您可以为
monitor.on()
创建一个包装器,以捕获您的液位值:
Monitor.prototype.onWithLevel = function(event, level, callback) {
this.on(event, function(res) {
return callback(res, level);
});
}
function writeLog(res, level){
if (level=='info'){
logger.info(getVarLine(res));
} else {
logger.error(getVarLine(res));
}
}
function pingServer(item){
var monitor = new Monitor({
website: item.url,
interval: interval
});
monitor.onWithLevel('up', level, writeLog);
monitor.onWithLevel('down', level, writeLog);
}
Here's how you could achieve what you want (ie pass an argument to a callback): 这是实现所需内容的方法(即,将参数传递给回调):
monitor.on('up', writeLog('info'));
monitor.on('down', writeLog('error'));
// You pass the log level
function writeLog(level) {
// and you return a function that will be called
// with your data when the event is triggered
return function(res) {
if (level=='info'){
logger.info(getVarLine(res));
} else {
logger.error(getVarLine(res));
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.