我正在使用 Splunk Javascript API 来访问其某些功能,但我无法理解回调背后的 JavaScript 概念。
文档中的示例:
var http = new splunkjs.ProxyHttp("/proxy");
var service = new splunkjs.Service(http, {
username: username,
password: password,
scheme: scheme,
host: host,
port: port,
version: version
});
Async.chain([
// First, we log in
function(done) {
service.login(done);
},
// Perform the search
function(success, done) {
if (!success) {
done("Error logging in");
}
service.search("search index=_internal | head 3", {}, done);
},
// Wait until the job is done
function(job, done) {
Async.whilst(
// Loop until it is done
function() { return !job.properties().isDone; },
// Refresh the job on every iteration, but sleep for 1 second
function(iterationDone) {
Async.sleep(1000, function() {
// Refresh the job and note how many events we've looked at so far
job.fetch(function(err) {
console.log("-- fetching, " + (job.properties().eventCount || 0) + " events so far");
iterationDone();
});
});
},
// When we're done, just pass the job forward
function(err) {
console.log("-- job done --");
done(err, job);
}
);
},
// Print out the statistics and get the results
function(job, done) {
// Print out the statics
console.log("Job Statistics: ");
console.log(" Event Count: " + job.properties().eventCount);
console.log(" Disk Usage: " + job.properties().diskUsage + " bytes");
console.log(" Priority: " + job.properties().priority);
// Ask the server for the results
job.results({}, done);
},
// Print the raw results out
function(results, job, done) {
// Find the index of the fields we want
var rawIndex = utils.indexOf(results.fields, "_raw");
var sourcetypeIndex = utils.indexOf(results.fields, "sourcetype");
var userIndex = utils.indexOf(results.fields, "user");
// Print out each result and the key-value pairs we want
console.log("Results: ");
for(var i = 0; i < results.rows.length; i++) {
console.log(" Result " + i + ": ");
console.log(" sourcetype: " + results.rows[i][sourcetypeIndex]);
console.log(" user: " + results.rows[i][userIndex]);
console.log(" _raw: " + results.rows[i][rawIndex]);
}
// Once we're done, cancel the job.
job.cancel(done);
}
],
function(err) {
callback(err);
}
);
Async.chain 已定义 here为root.chain = function(tasks,callback)
。我的理解是tasks数组中有5个函数,它们依次执行,并将结果从一个函数传递到另一个函数。
但是我不明白“done”、“success”、“job”和“results”是如何以及在哪里定义的,或者它们是如何在其函数体内用作参数的?
function(success, done) {
if (!success) {
done("Error logging in");
}
service.search("search index=_internal | head 3", {}, done);
}
这里,它如何测试是否成功,并将字符串传递给done()?
这两个函数是如何实现的
function(job, done) {// Print out the statics ..}
&
function(results, job, done) { .. }
将结果数据从第一个函数传递到第二个函数?
很抱歉问了这么长的问题。
最佳答案
在 Javascript 中,函数创建新的作用域。这意味着传递的参数在传递给函数之前的命名并不重要。
var awesomeName = 'bob';
hi(awesomeName);
// name === undefined
function hi(name) {
// name === 'bob';
console.log('hi', name); // Outputs: 'hi bob' in console
}
// name === undefined
正如您所说,每个任务都会调用下一个任务作为回调。 最后一个参数始终是下一个任务函数/回调。这意味着 Async.chain 可能会在调用每个任务函数之前自动将回调添加到参数末尾。 done
只是分配给回调的常规名称。同样,其他参数只是前一个任务传递的参数的描述性名称。为了了解为什么它们以这种方式命名,您应该查看调用回调的函数。
例如:
service.login(done)
中可能有某种代码可以执行以下操作:
login: function(callback) {
var successful;
// Do Login logic here and assign true/false to successful
callback(successful);
}
回调是链中的下一个任务,有两个参数:success
和 done
。 success
只是 login
传递给它的第一个参数。 Async.chain
始终传递另一个参数作为最后一个参数:下一个任务函数,按照惯例,它被分配了名称 done
。您可以在每个函数中将其命名为任何您想要的名称,只要您在函数中以相同的名称引用它即可。
function cb(success, fuzzyCallback) {
if (!success) {
fuzzyCallback('Error!');
}
fuzzyCallback(null);
}
关于JavaScript 回调和 Splunk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31172775/