所以我试图宣传splunk-sdk库,但似乎我不明白promisifying如何工作,至少与协同程序有关。
注意:我正在使用bluebirdjs
以下是我一直尝试在协程中重新创建的过程:
// Search everything and return the first 100 results
var searchQuery = "search * | head 100";
// Set the search parameters
var searchParams = {
exec_mode: "blocking",
earliest_time: "2012-06-20T16:27:43.000-07:00"
};
// A blocking search returns the job's SID when the search is done
console.log("Wait for the search to finish...");
// Run a blocking search and get back a job
service.search(
searchQuery,
searchParams,
function(err, job) {
console.log("...done!\n");
// Get the job from the server to display more info
job.fetch(function(err){
// Display properties of the job
console.log("Search job properties\n---------------------");
console.log("Search job ID: " + job.sid);
console.log("The number of events: " + job.properties().eventCount);
console.log("The number of results: " + job.properties().resultCount);
console.log("Search duration: " + job.properties().runDuration + " seconds");
console.log("This job expires in: " + job.properties().ttl + " seconds");
// Get the results and display them
job.results({}, function(err, results) {
var fields = results.fields;
var rows = results.rows;
for(var i = 0; i < rows.length; i++) {
var values = rows[i];
console.log("Row " + i + ": ");
for(var j = 0; j < values.length; j++) {
var field = fields[j];
var value = values[j];
console.log(" " + field + ": " + value);
}
}
})
});
}
);
来自this教程
这是我的努力所产生的(Pun部分意图?)
"use strict";
//Get the splunk sdk
var splunkjs = require('splunk-sdk');
//get promise dependencies
Promise = require("bluebird");
//get request dependencies
var request = require('request-promise');
//Promisify dependencies
splunkjs = Promise.promisifyAll(splunkjs);
//create a promise generator
exports.splunkTest = Promise.coroutine(function*(){
try{
// Create a Service instance and log in
let service = yield Promise.promisifyAll(Promise.resolve(new splunkjs.Service({
username:"user",
password:"password",
scheme:"https",
host:"host",
port:"8089",
version:"6.3.2"
})));
// Pass the search query
let searchQuery = "search * | head 100";
let searchParams = {
exec_mode: "blocking"
};
let job = yield Promise.resolve(service.search(searchQuery,searchParams,function(err,job){return console.log("running");return job;}));
//JOB DOESN'T CONTAIN FUNCTIONS!!! :(
return yield Promise.resolve(job);
} catch (e){
console.log(e)
}
});
作业对象在我的,可能是错误的尝试将该过程转换为协程中没有任何功能。
有人可以提供一些一般指导吗?
答案 0 :(得分:1)
splunkjs.Service
构造函数不会返回一个承诺,因此您不必yield
它。您确实希望在其上运行promisifyAll
,以宣传其所有方法。
所以:
let service = Promise.promisifyAll(new splunkjs.Service({...}));
接下来,您要调用search
的已宣传版本,该版本将被称为searchAsync
。由于这将返回一个承诺,你将产生它(并且你不会通过回调);
let job = yield service.searchAsync(...);
最后,您只需返回要返回的内容:
return job;