How to run functions sequentially in Angular 2/4 Typescript
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Running functions sequentially in Angular matters only when the work is asynchronous. If the functions are synchronous, JavaScript already runs them in order. The real problem appears when each step involves a promise, an HTTP request, or an observable that must finish before the next step starts.
Sequential Execution with async and await
If your functions return promises, async and await make sequence explicit and readable.
Each await pauses the sequence until that promise settles. That is what forces true ordering.
Sequential Execution with RxJS
Angular code often works with observables instead of promises, especially for HTTP calls. In that case, concatMap is the usual operator for strict sequencing because it waits for one inner observable to complete before subscribing to the next one.
This is the observable equivalent of an ordered promise chain.
Do Not Start Work Too Early
A common mistake is to create several async operations first and only await them later:
This does not enforce true sequence because both operations were started before either await. If the second task depends on the first, create it only after the first one finishes.
Error Handling Belongs in the Same Flow
Sequential workflows are easier to reason about when failures stop the chain clearly. With promises, wrap the sequence in try and catch. With observables, handle the error in subscribe or with an operator such as catchError.
That prevents later steps from running against incomplete state or partial data.
Pick the Pattern That Matches the Codebase
If the code already returns promises, use async and await. If the code already uses RxJS heavily, staying inside the observable pipeline is often cleaner than converting everything to promises just for sequencing.
The important choice is not Angular version. The important choice is which async abstraction your code already uses and how much control you need over cancellation, composition, and subscription lifetime.
Synchronous Functions Need No Async Tooling
If the functions are ordinary synchronous methods, just call them in order:
Adding promises or observables to purely synchronous code only makes the control flow harder to read.
Common Pitfalls
Assuming normal function order is enough when the real work is asynchronous causes race conditions.
Starting multiple async operations first and then awaiting them later defeats true sequential execution.
Using mergeMap when strict ordering is required allows concurrency when you did not want it.
Mixing promises and observables carelessly can make execution order and error flow hard to follow.
Adding async abstractions to synchronous code is unnecessary complexity.
Summary
- Sequential execution matters when the functions are asynchronous, not when they are plain synchronous calls.
- Use
asyncandawaitfor promise-based workflows. - Use
concatMapfor observable-based workflows in Angular. - Create the next async step only after the previous one finishes if order matters.
- Keep error handling inside the same control flow so failed steps stop the sequence cleanly.

