javascript - Multiple steps to process data, what's good pattern for JS/Angular? -
imagine have few steps process data. first download it, smth else , on many times. code in c# several lines - 1 each step.
as understand now, in js/angular this:
function prepateandgo() { loaddata() .$promise.then((loadeddata) => { preparedata(loadeddata).$promise().then((prepareddata) => { preprocessdata(prepareddata).$promise().then((preprocesseddata) => { anddosmthelse(preprocesseddata).$promise().then((anddosmthelsedata) => { makeupdata(anddosmthelsedata).$promise().then((makeupeddata) => { console.log('finally, loaded , processed, lets go'); }); }); }); }); }); }
isn't there more beautiful pattern? what's common solution against spaghetti?
what need create utility functions return promise. example:
function preparedata(data){ // data , return promise return $q.when(data); }
then can chain promises nicely. example:
loaddata() .then(preparedata) .then(preprocessdata) .then(anddosmthelse) .then(makeupdata) .then(function(madedata){ console.log('finally, loaded , processed, lets go'); });
note: code off top of head, should started
Comments
Post a Comment