-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4-async-await.js
33 lines (20 loc) · 961 Bytes
/
4-async-await.js
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
const { default: fetch } = require("node-fetch");
/* The Async-Await Approach was introduced in ES8 (ECMAScript 2017) */
/*
Like Generators, this approach allows execution to be paused and resumed.
In essence, these features are syntactic sugar on top of promises.
An async function can also be decomposed into a generator/promise implementation.
The await keyword pauses the async function execution until the Promise
is resolved or rejected. Other tasks are still able to run in the meantime.
*/
const URL = 'https://cat-fact.herokuapp.com/facts/random';
//alternate function declaration:
//async function getCatFact(){
const getCatFact = async() => {
const response = await fetch(URL);
const catFact = await response.json();
console.log('then once we receive the catFact we continue: ', catFact);
return catFact
}
const result = getCatFact();
console.log('Result of calling getCatFact(): ', result); //returns a pending promise