-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
47 lines (40 loc) · 1.49 KB
/
index.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
(function(context) {
async function asyncFetchDemo() {
console.log('Fetch async initiating');
const responsePromise = await fetch('https://api.github.com/search/repositories?sort=stars&order=desc&q=art%20o%20fjs')
const responseJSON = await responsePromise.json();
console.log(responseJSON);
console.log('Fetch async responsed successfully');
}
function regularFetchDemo() {
console.log('Fetch regular initiating');
fetch('https://api.github.com/search/repositories?sort=stars&order=desc&q=art%20o%20fjs')
.then(x => x.json())
.then(x => console.log(x));
console.log('Fetch regular responsed successfully');
}
function getPromise(value) {
return new Promise((resolve, reject) =>
setTimeout(() =>
resolve(value), 1000) );
}
async function asyncPromise() {
// await can be only used in async functions
console.log('asyncPromise initiated');
const result1 = await getPromise(100);
console.log('Result 1 recevied');
const result2 = await getPromise(200);
console.log('Result 2 recevied');
console.log(result1 + result2);
console.log('After result is printed');
}
function demo() {
console.log('\n\nASYNC AWAIT DEMO');
asyncFetchDemo();
console.log('After asyncFetchDemo is called');
regularFetchDemo();
console.log('After regularFetchDemo is called');
asyncPromise();
};
(context || this).demoLibs['async-await'] = demo;
})(window);