-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2_async_await.ts
More file actions
45 lines (39 loc) · 1.87 KB
/
Copy path2_async_await.ts
File metadata and controls
45 lines (39 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { Gross, Net, Project, Tax } from "./0_null_checking";
/* Let's refactor our calculate function to no longer
return an Optional, but a Promise instead.
Instead of checking of the Optional is empty, we
could now check if the promise rejects.
*/
declare function calculateNetPromise(project: Project): Promise<Net>
declare function calculateTaxPromise(project: Project, net: Net): Promise<Tax>
declare function calculateGrossPromise(net: Net, tax: Tax): Promise<Gross>
/* Promises have a `then` method. For us this works just like the
`flatMap` method from before.
We can therefor rewrite our function like this:
*/
function calculatePricePromise(project: Project): Promise<[Net, Tax, Gross]> {
return calculateNetPromise(project)
.then(net =>
calculateTaxPromise(project, net)
.then(tax =>
calculateGrossPromise(net, tax)
.then(gross => [net, tax, gross])));
}
/* Notice how we only had to swap `flatMap` with `then` and `Optional` with `Promise`.
Still, we have not gained anything, yet. Our IDE is already suggesting a transformation,
though. Let's apply it.
*/
async function calculatePriceAsync(project: Project): Promise<[Net, Tax, Gross]> {
const net = await calculateNetPromise(project);
const tax = await calculateTaxPromise(project, net);
const gross = await calculateGrossPromise(net, tax);
return [net, tax, gross];
}
/* Wow, this is much more readable!
One issue with this is that our function needs to be async.
Can we have the same syntax but without async?
Sadly no, at least not exactly like that.
Next, you can check out how the do-notation looks in Haskell (or C#/F#/Kotlin)
where it can be use for any type implements the required interface.
Additionally, you can check out an emulation of the do-notation in TypeScript.
*/