jquery - JavaScript Callbacks and Splunk -


i in process of using splunk javascript api gain access of functionality, i'm having trouble understanding javascript concepts behind callbacks.

an example docs:

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, log in         function(done) {             service.login(done);         },         // perform search         function(success, done) {             if (!success) {                 done("error logging in");             }              service.search("search index=_internal | head 3", {}, done);         },         // wait until job done         function(job, done) {             async.whilst(                 // loop until done                 function() { return !job.properties().isdone; },                 // refresh job on every iteration, sleep 1 second                 function(iterationdone) {                     async.sleep(1000, function() {                         // refresh job , note how many events we've looked @ far                         job.fetch(function(err) {                             console.log("-- fetching, " + (job.properties().eventcount || 0) + " events far");                             iterationdone();                         });                     });                 },                 // when we're done, pass job forward                 function(err) {                     console.log("-- job done --");                     done(err, job);                 }             );         },         // print out statistics , results         function(job, done) {             // print out 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 server results             job.results({}, done);         },         // print raw results out         function(results, job, done) {             // find index of fields 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 , key-value pairs want             console.log("results: ");             for(var = 0; < results.rows.length; i++) {                 console.log("  result " + + ": ");                 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 job.             job.cancel(done);         }     ],     function(err) {         callback(err);             } ); 

async.chain defined here being root.chain = function(tasks, callback). understanding there 5 functions in tasks array executed 1 after other, , pass results 1 other.

however not understand how , "done", "success","job" , "results" defined, or how used arguments within body of functions?

  function(success, done) {             if (!success) {                 done("error logging in");             }              service.search("search index=_internal | head 3", {}, done);   } 

here, how testing against success, , passing string done()?

and how 2 functions

function(job, done) {// print out statics ..} & function(results, job, done) { .. } 

pass results data first function second?

apologies long question.

in javascript, functions create new scope. means not matter passed arguments named before passed function.

var awesomename = 'bob';  hi(awesomename);  // name === undefined  function hi(name) {     // name === 'bob';     console.log('hi', name); // outputs: 'hi bob' in console }  // name === undefined 

as said, each task calls next task callback. last argument next task function/callback. means async.chain automagically adds callbacks end of arguments before calling each task function. done conventional name assign callback. similarly, other arguments descriptive names arguments passed previous task. in order see why named way, should @ function calling callback.

for example:

service.login(done) has kind of code in this:

login: function(callback) {     var successful;      // login logic here , assign true/false successful      callback(successful); } 

the callback next task in chain , has 2 arguments, success , done. success first argument login passed it. async.chain passes argument last argument: next task function, assigned name done convention. name whatever want within each function, long refer same name within function.

function cb(success, fuzzycallback) {   if (!success) {     fuzzycallback('error!');   }    fuzzycallback(null); } 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -