Refactoring From Callback Hell To Observables: in Typescript + Angular
Refactoring From Callback Hell To Observables: in Typescript + Angular
Marian Stanciu
In TypeScript + Angular Victor Rentea
Victor Rentea
Java Champion
Lead Architect at
VictorRentea.ro
Independent
Technical Trainer & Coach
6 years 1300 devs 180+ days 30 companies
[email protected] VictorRentea.ro
Marian Stanciu
Technical Lead
Full-Stack Developer
Internal Mentor/Coach
[email protected]
JavaScript is Single Threaded
Avoids race conditions
+
Background Tasks
REST calls at a SPA
=
5 V
Callbacks @VictorRentea @StanciuMarianM1
Init Coupon Service
Submit
Get Current User Get All Countries
Validate ReceiptID
Legend: REST
Preselect User
Confirm Dialog
Country
USER
SYNC
Get Stores for City Display Coupon
6 V @VictorRentea @StanciuMarianM1
7
looking back
8 @VictorRentea @StanciuMarianM1
Clean Code + TypeScript
Controller methods should access fields directly ng
Promise.all forkJoin
10 V @VictorRentea @StanciuMarianM1
Use types. Never “any” or {}.
We TypeScript
12 M @VictorRentea @StanciuMarianM1
async asyncAwait() {
// stuff1
await this.getPromiseA();
// stuff2
let b = await this.getPromiseB();
// stuff3 with b equivalent to
}
promiseChaining() { callbackHell() {
// stuff1 // stuff1
this.getPromiseA().then(() => { this.getPromiseA().then(() => {
// stuff2 // stuff2
return this.getPromiseB(); this.getPromiseB().then(b => {
}).then(b => { // stuff3 with b
// stuff3 with b });
}); });
} }
13 V @VictorRentea @StanciuMarianM1
Multi-Task Elegantly
on 1 Thread!
Questions?