forked from codediodeio/async-await-pro-tips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
6-sugar.ts
33 lines (25 loc) · 767 Bytes
/
6-sugar.ts
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
import { getFruit } from './3-async-await';
const fruits = ['peach', 'pineapple', 'strawberry'];
// Will pause each step for promise to be resolved
const fruitLoop = async () => {
for (const f of fruits) {
const emoji = await getFruit(f);
log(emoji);
}
// For running everything concurrently
// for await (const f of fruits) {
// const emoji = getFruit(f);
// log(emoji);
// }
};
const fruitInspection = async () => {
if ((await getFruit('peach')) === '🍑') {
console.log('looks peachy!');
}
};
import fetch from 'node-fetch';
const getTodo = async () => {
const res = await fetch('https://jsonplaceholder.typicode.com/todos/1');
const { title, userId, body } = await res.json();
console.log(title, userId, body);
};