var html = ''; request.on('response', function (response) { response.on('data', function (chunk) { html = html + chunk; }); response.on('end', function() { //- parse(html, function(data){ //- , addToDatabase(data, function() { doSomething(); }) }); }); });
var html = ''; var responceOnEnd = function() { parse(html, parsed); } var parsed = function(data){ addToDatabase(data, addedToDatabase) } var addedToDatabase = function() { doSomething(); } request.on('response', function (response) { response.on('data', function (chunk) { html = html + chunk; }); response.on('end', responceOnEnd); });
wait(function(runNext){ request.on('response', runNext); }).wait(function(runNext, response){ response.on('data', function (chunk) { html = html + chunk; }); response.on('end', function() { runNext(html); }); }).wait(function(runNext, html){ parse(html, runNext); }).wait(function(runNext, data){ addToDatabase(data, runNext); }).wait(function(){ doSomething(); })
//first — , wait = function(first){ // return new (function(){ var self = this; var callback = function(){ var args; if(self.deferred.length) { /* */ args = [].slice.call(arguments); /* - */ args.unshift(callback); // self.deferred[0].apply(self, args); // self.deferred.shift(); } } this.deferred = []; // this.wait = function(run){ // this.deferred.push(run); // this return self; } first(callback); // }); }
wait(function(runNext){ log(' '); $('#div1').animate({ top: 30 }, 1000, function(){ // - runNext(1,2); }); }).wait(function(runNext, a, b){ // log(' , a='+a+' b='+b ); $('#div2').animate({ top: 50 }, 1000, runNext); }).wait(function(runNext){ log(' '); setTimeout(function(){ log(' ') runNext(); }, 2000); }).wait(function(runNext){ log(' '); $('#div3').animate({ left: 50 }, 1000, runNext); }).wait(function(runNext){ log(' '); $('#div1').animate({ top: 0, left: 45 }, 1000, runNext); }).wait(function(){ log(''); });
Run an example on JSFidleSource: https://habr.com/ru/post/134823/
All Articles