-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
138 lines (114 loc) · 4.21 KB
/
index.html
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Async/Await in Swift</title>
<link rel="canonical" href="https://async-await-in-swift.netlify.app/" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css"
integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous" />
<style>
.bd-placeholder-img {
font-size: 1.125rem;
text-anchor: middle;
-webkit-user-select: none;
-moz-user-select: none;
user-select: none;
}
@media (min-width: 768px) {
.bd-placeholder-img-lg {
font-size: 3.5rem;
}
}
body {
background-color: #f5f5f5;
color: #333333;
}
.fa.fa-github {
color: #999999;
}
.code-snippet-sm {
width: 100%;
height: 360px;
}
@media only screen and (max-width: 480px) {
.code-snippet-sm {
width: 100%;
height: 720px;
}
}
.code-snippet-md {
width: 100%;
height: 440px;
}
@media only screen and (max-width: 480px) {
.code-snippet-md {
width: 100%;
height: 880px;
}
}
</style>
</head>
<body>
<div class="col-lg-8 mx-auto p-3 py-md-5">
<h1>Async/Await in Swift</h1>
<p class="mt-4 lead">
Async/Await allow asynchronous code to be written as straight-line code.
</p>
<h3 class="mt-4">Convert callbacks to async/await</h3>
<div class="row mt-3">
<p class="fs-5 col-md-8 lead">
If you write code that makes a series of such asynchronous calls in a
callback-based API, you will easily fall into callback hell.
</p>
<iframe id="54380DAB-27D4-4B71-B591-0176C7C1D65C" class="code-snippet-md"
src="https://swiftfiddle.com/apkmiawsljfq3esfpmdcvuckcu/embedded/?fold=77-78&fold=83-84" frameborder="0">
</iframe>
</div>
<div class="row mt-4">
<p class="fs-5 col-md-8 lead">
Refactoring the code using <code>async/await</code>, it can be written
in a clear and straightforward manner as follows.
</p>
<iframe id="B6A1A6B3-433A-4736-9303-8F143955825E" class="code-snippet-sm"
src="https://swiftfiddle.com/lasd2zzadvc5ncjzeobxk2mzk4/embedded/?fold=56-57&fold=59-60&fold=61-63"
frameborder="0">
</iframe>
</div>
<h3 class="mt-4">Call <code>async/await</code> functions in parallel</h3>
<div class="row mt-3">
<p class="fs-5 col-md-8 lead">
The following code executes each
<code>fetchTodoDetail(id:)</code> function sequentially.
</p>
<iframe id="EB4A43EA-FEB2-47C0-AE06-F9637997A2EC" class="code-snippet-sm"
src="https://swiftfiddle.com/npphvpzfznfb7clshe4gjceed4/embedded/?fold=57-58&fold=60-61&fold=62-64"
frameborder="0">
</iframe>
</div>
<div class="row mt-4">
<p class="fs-5 col-md-8 lead">
To call each <code>fetchTodoDetail(id:)</code> function in parallel,
use <code>async let</code> to defer the calls, then use
<code>await</code> to resolve the values in later lines. They will be
executed in parallel at the line with the <code>await</code>.
</p>
<iframe id="25A3E751-3AD9-40F6-A8CD-1797FF9996E0" class="code-snippet-sm"
src="https://swiftfiddle.com/f4y3vrnftjdhloc26h4j4xn22m/embedded/?fold=57-58&fold=60-61&fold=62-64"
frameborder="0">
</iframe>
</div>
<footer class="pt-5 my-5 text-muted border-top">
<div style="text-align: center">
<a href="https://github.com/kishikawakatsumi/async-await-in-swift"><i class="fa fa-github fa-2x"></i></a>
</div>
</footer>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"
integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js"
integrity="sha384-Piv4xVNRyMGpqkS2by6br4gNJ7DXjqk09RmUpJ8jgGtD7zP9yug3goQfGII0yAns" crossorigin="anonymous">
</script>
<script src="https://kit.fontawesome.com/108aa91541.js" crossorigin="anonymous"></script>
</body>
</html>