-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync-await.js
37 lines (30 loc) · 1.12 KB
/
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
34
35
36
37
"use strict";
console.log("Script start:\n");
async function getRandomNumber(callerFn) {
console.log(`\nFunction ${callerFn} started ...`);
const randNum = Math.round(Math.random() * 100);
console.log(`Random number generated by ${callerFn}: ${randNum}`);
return randNum;
}
async function asyncWithoutAwait(callerFn) {
console.log(`\nFunction ${callerFn} started ...`);
const x = getRandomNumber(`getRandomNumber called in ${callerFn}`);
console.log(x);
console.log(`Function ${callerFn} ended.\n`);
}
async function asyncWithAwait(callerFn) {
console.log(`\nFunction ${callerFn} started ...`);
const x = await getRandomNumber(`getRandomNumber called in ${callerFn}`);
console.log(x);
console.log(`Function ${callerFn} ended.\n`);
}
async function asyncWithAwait2(callerFn) {
console.log(`\nFunction ${callerFn} started ...`);
const x = getRandomNumber(`getRandomNumber called in ${callerFn}`);
console.log(await x);
console.log(`Function ${callerFn} ended.\n`);
}
asyncWithAwait("asyncWithAwait");
asyncWithAwait2("asyncWithAwait2");
asyncWithoutAwait("asyncWithoutAwait");
console.log("\nScript end.\n");