0% found this document useful (0 votes)
168 views14 pages

Refactoring From Callback Hell To Observables: in Typescript + Angular

This document discusses refactoring callback code to use observables. It begins by introducing Marian Stanciu and Victor Rentea, who will discuss this topic. The next sections provide background on Victor, including his experience in software engineering practices like XP, clean code, and TDD. The document then covers some key points about callbacks versus observables, including that observables allow handling background tasks and avoiding race conditions while maintaining single-threaded behavior. It concludes by inviting the audience to join further discussions from Victor and Marian.

Uploaded by

Monika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
168 views14 pages

Refactoring From Callback Hell To Observables: in Typescript + Angular

This document discusses refactoring callback code to use observables. It begins by introducing Marian Stanciu and Victor Rentea, who will discuss this topic. The next sections provide background on Victor, including his experience in software engineering practices like XP, clean code, and TDD. The document then covers some key points about callbacks versus observables, including that observables allow handling background tasks and avoiding race conditions while maintaining single-threaded behavior. It concludes by inviting the audience to join further discussions from Victor and Marian.

Uploaded by

Monika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Refactoring

from Callback Hell


to Observables

Marian Stanciu
In TypeScript + Angular Victor Rentea
Victor Rentea
Java Champion

Lead Architect at

Software Craftsman Founder of:


XP: Pair Programming, Clean Code, TDD

Clean Code Evangelist


30+ talks, 10+ meetups

VictorRentea.ro
Independent
Technical Trainer & Coach
6 years 1300 devs 180+ days 30 companies

Spring Hibernate Java 8

Design Patterns Architecture, DDD

Clean Code Unit Testing, TDD

Java Performance Scala and much more…


Posting daily on

[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

Get Cities for Country Request Coupon

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

Don't subscribe() in subscribe(): Extract methods instead

Learn the Array API: filter, find, map, …

Prefer local variables instead of fields


9 V @VictorRentea @StanciuMarianM1
Async
$q Promise Constructs Observable rx
One Async Event Stream of Async Events
(But an HTTP request only fires ONCE)

Promise.all forkJoin

await <> Cancellable


UI events:

10 V @VictorRentea @StanciuMarianM1
Use types. Never “any” or {}.
We TypeScript

Group your fields in data structures.


Then, generate them.

Keep your API in sync


using swagger
11 M @VictorRentea @StanciuMarianM1
Swagger Generated 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?

Join us! Posting daily on:


@IBM

14 Trainings: VictorRentea.ro / [email protected] @VictorRentea @StanciuMarianM1

You might also like