javascript - Avoiding repetition when using callback functions and asynchronous requests -
i writing javascript/jquery code requires me make lots of requests various apis. have come across problem many times , basic example of 1 instance of it.
the code makes asynchronous request server , if condition returned data not met, code makes request. second request has callback contains of same logic parent. know avoiding repetition calling returnfunction in each callback there way avoid altogether?
var container = [];  $.getjson("http://www.url.com/api/?callback=?",  { data: "mydata" },  function(data) {     if(!data.length) {         // no results     } else {          // put results array         $(data).each(function(k, v){             container.push(v.some_data);         });          // if query did not return enough results         if(data.length < limit) {             var number_missing = limit - data.length;              // more results , append array             myfunctiontogetsomethingelse(number_missing, function(response){                  // add these results array                 $(response).each(function(k, v){                     container.push(v.some_data);                 });                  // data                 returnfunction(response);             });          } else {             // result             returnfunction(data);         }     } });   how recommend avoid repetition of code inside callbacks? possible way?
there library delivering "stratified javascript". oddly enough, wraps asynchronous calls sequential paradigm. square 1 :)
take @ oni labs.
Comments
Post a Comment