javascript - How can I call a function after several callbacks have been called -


i need push data new array database, return promise object. 5 times need call. this.

 var items = [];     function setdata() {     facilityapis.getalllistitems.get({       listname: "samples"     }).     $promise.then(function(data) {       alert("1");       (var j = 0; j < data.items.length; j++) {         items.push(data.items[j]);       }       console.log(items);     }).then(function() {       facilityapis.getalllistitems.get({         listname: "controls"       }).       $promise.then(function(data) {         alert("2");         (var j = 0; j < data.items.length; j++) {           items.push(data.items[j]);         }         console.log(items);       });     }).then(function() {       facilityapis.getalllistitems.get({         listname: "fibroblast"       }).       $promise.then(function(data) {         alert("3");         (var j = 0; j < data.items.length; j++) {           items.push(data.items[j]);         }         console.log(items);       });     }).then(function() {       facilityapis.getalllistitems.get({         listname: "pbmc"       }).       $promise.then(function(data) {         alert("4");         (var j = 0; j < data.items.length; j++) {           items.push(data.items[j]);         }         console.log(items);       });     }).then(function() {       facilityapis.getalllistitems.get({         listname: "ips cell lines"       }).       $promise.then(function(data) {         alert("5");         (var j = 0; j < data.items.length; j++) {           items.push(data.items[j]);         }         console.log(items);       });     });   }

but if want use items array after of data pushed it, such call splitdata(items). how can that. thank you.

so this?

var items = [];  some_fn(...)     .then(function(data) { [push data items] })     .then(function() {         some_fn(...).then(function(data) { [push data items] });     })     .then(function() {         some_fn(...).then(function(data) { [push data items] });     })     .then(function() {         some_fn(...).then(function(data) { [push data items] });     })     .then(function() {         some_fn(...).then(function(data) { [push data items] });     })     .then(function() {         ...use items somehow after _all_ data has been pushed it...     }) 

if so, problem ".then chain" isn't chain: "top level .then"s executed 1 after another, inside each of them start new asynchronous operation separately. each of these push data items, end result cannot captured in last ".then".

if attach multiple "fulfilled" callbacks same promise, , don't return promise them, execute 1 after other, when first promise fulfilled:

promise   .then(function() { foo; }  // executed when promise fulfilled   .then(function() { bar; }  // executed right after 

if run other async operations in callbacks, , want create chain, need return promise callback:

promise    .then(function() { return promise_foo; }  // executed when promise fulfilled    .then(function() { return promise_bar; }  // executed when promise_foo fulfilled 

(for details, see promise resolution procedure in spec.)

so original example reorganised create single chain:

some_fn(...)     .then(function(data) { [push data items] })     .then(function() {         return some_fn(...).then(function(data) { [push data items] });     })     .then(function() {         return some_fn(...).then(function(data) { [push data items] });     })     .then(function() {         return some_fn(...).then(function(data) { [push data items] });     })     .then(function() {         return some_fn(...).then(function(data) { [push data items] });     })     .then(function() {         ...all operations have finished, can use items here...     }) 

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -