Javascript 无法提出晦涩难懂的库,然后在协同程序中使用它

Javascript 无法提出晦涩难懂的库,然后在协同程序中使用它,javascript,promise,bluebird,Javascript,Promise,Bluebird,因此,我试图推荐splunk sdk库,但似乎我不明白promisify是如何工作的,至少在协同程序中是如此 注意:我使用的是bluebirdjs 以下是我一直试图在协同程序中重新创建的过程: // Search everything and return the first 100 results var searchQuery = "search * | head 100"; // Set the search parameters var searchParams = { exec_

因此,我试图推荐splunk sdk库,但似乎我不明白promisify是如何工作的,至少在协同程序中是如此

注意:我使用的是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);
          }
        }
      })

    });

  }
);
作业对象在我试图将进程转换为协同程序时没有任何功能,这可能是错误的


有人能提供一些一般性的指导吗?

这个
splunkjs.Service
构造函数不会返回承诺,所以你不必
屈服于它。您确实希望在其上运行
promisifyAll
,以promisify其所有方法

因此:

接下来,您要调用
search
的预期版本,该版本将被称为
searchAsync
。因为这将返回一个承诺,所以您将放弃它(并且您不会通过回调)

最后,您只需返回您想要返回的内容:

return job;
let service = Promise.promisifyAll(new splunkjs.Service({...}));
let job = yield service.searchAsync(...);
return job;