1function resolveAfter2Seconds() {
2 return new Promise(resolve => {
3 setTimeout(() => {
4 resolve('resolved');
5 }, 2000);
6 });
7}
8
9//async function:
10async function asyncCall() {
11 console.log('calling');
12 const result = await resolveAfter2Seconds();
13 console.log(result);
14 // expected output: 'resolved'
15}
16
17asyncCall();
1using System;
2using System.Threading.Tasks;
3
4namespace AsyncBreakfast
5{
6 class Program
7 {
8 static void Main(string[] args)
9 {
10 Coffee cup = PourCoffee();
11 Console.WriteLine("coffee is ready");
12
13 Egg eggs = FryEggs(2);
14 Console.WriteLine("eggs are ready");
15
16 Bacon bacon = FryBacon(3);
17 Console.WriteLine("bacon is ready");
18
19 Toast toast = ToastBread(2);
20 ApplyButter(toast);
21 ApplyJam(toast);
22 Console.WriteLine("toast is ready");
23
24 Juice oj = PourOJ();
25 Console.WriteLine("oj is ready");
26 Console.WriteLine("Breakfast is ready!");
27 }
28
29 private static Juice PourOJ()
30 {
31 Console.WriteLine("Pouring orange juice");
32 return new Juice();
33 }
34
35 private static void ApplyJam(Toast toast) =>
36 Console.WriteLine("Putting jam on the toast");
37
38 private static void ApplyButter(Toast toast) =>
39 Console.WriteLine("Putting butter on the toast");
40
41 private static Toast ToastBread(int slices)
42 {
43 for (int slice = 0; slice < slices; slice++)
44 {
45 Console.WriteLine("Putting a slice of bread in the toaster");
46 }
47 Console.WriteLine("Start toasting...");
48 Task.Delay(3000).Wait();
49 Console.WriteLine("Remove toast from toaster");
50
51 return new Toast();
52 }
53
54 private static Bacon FryBacon(int slices)
55 {
56 Console.WriteLine($"putting {slices} slices of bacon in the pan");
57 Console.WriteLine("cooking first side of bacon...");
58 Task.Delay(3000).Wait();
59 for (int slice = 0; slice < slices; slice++)
60 {
61 Console.WriteLine("flipping a slice of bacon");
62 }
63 Console.WriteLine("cooking the second side of bacon...");
64 Task.Delay(3000).Wait();
65 Console.WriteLine("Put bacon on plate");
66
67 return new Bacon();
68 }
69
70 private static Egg FryEggs(int howMany)
71 {
72 Console.WriteLine("Warming the egg pan...");
73 Task.Delay(3000).Wait();
74 Console.WriteLine($"cracking {howMany} eggs");
75 Console.WriteLine("cooking the eggs ...");
76 Task.Delay(3000).Wait();
77 Console.WriteLine("Put eggs on plate");
78
79 return new Egg();
80 }
81
82 private static Coffee PourCoffee()
83 {
84 Console.WriteLine("Pouring coffee");
85 return new Coffee();
86 }
87 }
88}
89
1async function f() {
2
3 try {
4 let response = await fetch('/no-user-here');
5 let user = await response.json();
6 } catch(err) {
7 // catches errors both in fetch and response.json
8 alert(err);
9 }
10}
11
12f();
1function resolveAfter2Seconds() {
2 return new Promise(resolve => {
3 setTimeout(() => {
4 resolve('resolved');
5 }, 2000);
6 });
7}
8
9async function asyncCall() {
10 console.log('calling');
11 const result = await resolveAfter2Seconds();
12 console.log(result);
13 // expected output: 'resolved'
14}
15
16asyncCall();
17
1function delayResult() {
2 return new Promise(resolve => {
3 setTimeout(() => {
4 resolve(‘Done’);
5 }, 5000)
6 })
7}
8async function getResult() {
9 let result = await delayResult();
10 return result;
11}
12getResult();