How can I make my code run sequentially? For example,
If I have a for loop which gets some data from a service, I want the n+1
iteration to run
You can do it with Promise.
someMethod() : Promise {
return new Promise((resolve) => {
for ( var i = 0; i < someLength; i++) {
// get some data
this.dataService.get(i).subscribe(data => {
// do something with the data
});
if(i==someLength-1){resolve();}
}
}).then(()=>{
//
console.log ('print me only after all iterations');
// ....
// some more lines of code
})
}