aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/conflicting/learn/javascript/asynchronous/index.html
blob: de99e1aed8c6ead056566a2c5871acaaa14f2948 (plain)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
---
title: 選擇正確的方法
slug: conflicting/Learn/JavaScript/Asynchronous
tags:
  - Beginner
  - Intervals
  - JavaScript
  - Learn
  - Optimize
  - Promises
  - async
  - asynchronous
  - await
  - requestAnimationFrame
  - setInterval
  - setTimeout
  - timeouts
original_slug: Learn/JavaScript/Asynchronous/Choosing_the_right_approach
---
<div>{{LearnSidebar}}</div>

<div>{{PreviousMenu("Learn/JavaScript/Asynchronous/Async_await", "Learn/JavaScript/Asynchronous")}}</div>

<p>在結束本單元前,我們將會從先前所討論過的不同程式技巧和功能,幫助您考量在甚麼情況下應該使用哪一個,並適當的提供一些有關常見的陷阱的建議及提醒。</p>

<table class="learn-box standard-table">
 <tbody>
  <tr>
   <th scope="row">Prerequisites:</th>
   <td>Basic computer literacy, a reasonable understanding of JavaScript fundamentals.</td>
  </tr>
  <tr>
   <th scope="row">Objective:</th>
   <td>To be able to make a sound choice of when to use different asynchronous programming techniques.</td>
  </tr>
 </tbody>
</table>

<h2 id="Asynchronous_callbacks">Asynchronous callbacks</h2>

<p>Generally found in old-style APIs, involves a function being passed into another function as a parameter, which is then invoked when an asynchronous operation has been completed, so that the callback can in turn do something with the result. This is the precursor to promises; it's not as efficient or flexible. Use only when necessary.</p>

<table class="standard-table">
 <caption>Useful for...</caption>
 <thead>
  <tr>
   <th scope="col">Single delayed operation</th>
   <th scope="col">Repeating operation</th>
   <th scope="col">Multiple sequential operations</th>
   <th scope="col">Multiple simultaneous operations</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>No</td>
   <td>Yes (recursive callbacks)</td>
   <td>Yes (nested callbacks)</td>
   <td>No</td>
  </tr>
 </tbody>
</table>

<h3 id="Code_example">Code example</h3>

<p>An example that loads a resource via the <a href="/en-US/docs/Web/API/XMLHttpRequest"><code>XMLHttpRequest</code> API</a> (<a href="https://mdn.github.io/learning-area/javascript/asynchronous/introducing/xhr-async-callback.html">run it live</a>, and <a href="https://github.com/mdn/learning-area/blob/master/javascript/asynchronous/introducing/xhr-async-callback.html">see the source</a>):</p>

<pre class="brush: js">function loadAsset(url, type, callback) {
  let xhr = new XMLHttpRequest();
  xhr.open('GET', url);
  xhr.responseType = type;

  xhr.onload = function() {
    callback(xhr.response);
  };

  xhr.send();
}

function displayImage(blob) {
  let objectURL = URL.createObjectURL(blob);

  let image = document.createElement('img');
  image.src = objectURL;
  document.body.appendChild(image);
}

loadAsset('coffee.jpg', 'blob', displayImage);</pre>

<h3 id="Pitfalls">Pitfalls</h3>

<ul>
 <li>Nested callbacks can be cumbersome and hard to read (i.e. "callback hell").</li>
 <li>Failure callbacks need to be called once for each level of nesting, whereas with promises you can just use a single <code>.catch()</code> block to handle the errors for the entire chain.</li>
 <li>Async callbacks just aren't very graceful.</li>
 <li>Promise callbacks are always called in the strict order they are placed in the event queue; async callbacks aren't.</li>
 <li>Async callbacks lose full control of how the function will be executed when passed to a third-party library.</li>
</ul>

<h3 id="Browser_compatibility">Browser compatibility</h3>

<p>Really good general support, although the exact support for callbacks in APIs depends on the particular API. Refer to the reference documentation for the API you're using for more specific support info.</p>

<h3 id="Further_information">Further information</h3>

<ul>
 <li><a href="/en-US/docs/Learn/JavaScript/Asynchronous/Introducing">Introducing asynchronous JavaScript</a>, in particular <a href="/en-US/docs/Learn/JavaScript/Asynchronous/Introducing#async_callbacks">Async callbacks</a></li>
</ul>

<h2 id="setTimeout">setTimeout()</h2>

<p><code><a href="/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout">setTimeout()</a></code> is a method that allows you to run a function after an arbitrary amount of time has passed.</p>

<table class="standard-table">
 <caption>Useful for...</caption>
 <thead>
  <tr>
   <th scope="col">Single delayed operation</th>
   <th scope="col">Repeating operation</th>
   <th scope="col">Multiple sequential operations</th>
   <th scope="col">Multiple simultaneous operations</th>
  </tr>
  <tr>
   <td>Yes</td>
   <td>Yes (recursive timeouts)</td>
   <td>Yes (nested timeouts)</td>
   <td>No</td>
  </tr>
 </thead>
</table>

<h3 id="Code_example_2">Code example</h3>

<p>Here the browser will wait two seconds before executing the anonymous function, then will display the alert message (<a href="https://mdn.github.io/learning-area/javascript/asynchronous/loops-and-intervals/simple-settimeout.html">see it running live</a>, and <a href="https://github.com/mdn/learning-area/blob/master/javascript/asynchronous/loops-and-intervals/simple-settimeout.html">see the source code</a>):</p>

<pre class="brush: js">let myGreeting = setTimeout(function() {
  alert('Hello, Mr. Universe!');
}, 2000)</pre>

<h3 id="Pitfalls_2">Pitfalls</h3>

<p>You can use recursive <code>setTimeout()</code> calls to run a function repeatedly in a similar fashion to <code>setInterval()</code>, using code like this:</p>

<pre class="brush: js">let i = 1;
setTimeout(function run() {
  console.log(i);
  i++;

  setTimeout(run, 100);
}, 100);</pre>

<p>There is a difference between recursive <code>setTimeout()</code> and <code>setInterval()</code>:</p>

<ul>
 <li>Recursive <code>setTimeout()</code> guarantees at least the specified amount of time (100ms in this example) will elapse between the executions; the code will run and then wait 100 milliseconds before it runs again. The interval will be the same regardless of how long the code takes to run.</li>
 <li>With <code>setInterval()</code>, the interval we choose <em>includes</em> the time taken to execute the code we want to run in. Let's say that the code takes 40 milliseconds to run — the interval then ends up being only 60 milliseconds.</li>
</ul>

<p>When your code has the potential to take longer to run than the time interval you’ve assigned, it’s better to use recursive <code>setTimeout()</code> — this will keep the time interval constant between executions regardless of how long the code takes to execute, and you won't get errors.</p>

<h3 id="Browser_compatibility_2">Browser compatibility</h3>

<p>{{Compat("api.WindowOrWorkerGlobalScope.setTimeout")}}</p>

<h3 id="Further_information_2">Further information</h3>

<ul>
 <li><a href="/en-US/docs/Learn/JavaScript/Asynchronous/Timeouts_and_intervals">Cooperative asynchronous JavaScript: Timeouts and intervals</a>, in particular <a href="/en-US/docs/Learn/JavaScript/Asynchronous/Timeouts_and_intervals#settimeout()">setTimeout()</a></li>
 <li><a href="/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout">setTimeout() reference</a></li>
</ul>

<h2 id="setInterval">setInterval()</h2>

<p><code><a href="/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval">setInterval()</a></code> is a method that allows you to run a function repeatedly with a set interval of time between each execution. Not as efficient as <code><a href="/en-US/docs/Web/API/window/requestAnimationFrame">requestAnimationFrame()</a></code>, but allows you to choose a running rate/frame rate.</p>

<table class="standard-table">
 <caption>Useful for...</caption>
 <thead>
  <tr>
   <th scope="col">Single delayed operation</th>
   <th scope="col">Repeating operation</th>
   <th scope="col">Multiple sequential operations</th>
   <th scope="col">Multiple simultaneous operations</th>
  </tr>
  <tr>
   <td>No</td>
   <td>Yes</td>
   <td>No (unless they are the same)</td>
   <td>No</td>
  </tr>
 </thead>
</table>

<h3 id="Code_example_3">Code example</h3>

<p>The following function creates a new <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date">Date()</a></code> object, extracts a time string out of it using <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleTimeString">toLocaleTimeString()</a></code>, and then displays it in the UI. We then run it once per second using <code>setInterval()</code>, creating the effect of a digital clock that updates once per second (<a href="https://mdn.github.io/learning-area/javascript/asynchronous/loops-and-intervals/setinterval-clock.html">see this live</a>, and also <a href="https://github.com/mdn/learning-area/blob/master/javascript/asynchronous/loops-and-intervals/setinterval-clock.html">see the source</a>):</p>

<pre class="brush: js">function displayTime() {
   let date = new Date();
   let time = date.toLocaleTimeString();
   document.getElementById('demo').textContent = time;
}

const createClock = setInterval(displayTime, 1000);</pre>

<h3 id="Pitfalls_3">Pitfalls</h3>

<ul>
 <li>The frame rate isn't optimized for the system the animation is running on, and can be somewhat inefficient. Unless you need to choose a specific (slower) framerate, it is generally better to use <code>requestAnimationFrame()</code>.</li>
</ul>

<h3 id="Browser_compatibility_3">Browser compatibility</h3>

<p>{{Compat("api.WindowOrWorkerGlobalScope.setInterval")}}</p>

<h3 id="Further_information_3">Further information</h3>

<ul>
 <li><a href="/en-US/docs/Learn/JavaScript/Asynchronous/Timeouts_and_intervals">Cooperative asynchronous JavaScript: Timeouts and intervals</a>, in particular <a href="/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval">setInterval()</a></li>
 <li><a href="/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval">setInterval() reference</a></li>
</ul>

<h2 id="requestAnimationFrame">requestAnimationFrame()</h2>

<p><code><a href="/en-US/docs/Web/API/window/requestAnimationFrame">requestAnimationFrame()</a></code> is a method that allows you to run a function repeatedly, and efficiently, at the best framerate available given the current browser/system. You should, if at all possible, use this instead of <code>setInterval()</code>/recursive <code>setTimeout()</code>, unless you need a specific framerate.</p>

<table class="standard-table">
 <caption>Useful for...</caption>
 <thead>
  <tr>
   <th scope="col">Single delayed operation</th>
   <th scope="col">Repeating operation</th>
   <th scope="col">Multiple sequential operations</th>
   <th scope="col">Multiple simultaneous operations</th>
  </tr>
  <tr>
   <td>No</td>
   <td>Yes</td>
   <td>No (unless it is the same one)</td>
   <td>No</td>
  </tr>
 </thead>
</table>

<h3 id="Code_example_4">Code example</h3>

<p>A simple animated spinner; you can find this <a href="https://mdn.github.io/learning-area/javascript/asynchronous/loops-and-intervals/simple-raf-spinner.html">example live on GitHub</a> (see the <a href="https://github.com/mdn/learning-area/blob/master/javascript/asynchronous/loops-and-intervals/simple-raf-spinner.html">source code</a> also):</p>

<pre class="brush: js">const spinner = document.querySelector('div');
let rotateCount = 0;
let startTime = null;
let rAF;

function draw(timestamp) {
    if(!startTime) {
        startTime = timestamp;
    }

    rotateCount = (timestamp - startTime) / 3;

    if(rotateCount &gt; 359) {
        rotateCount %= 360;
    }

    spinner.style.transform = 'rotate(' + rotateCount + 'deg)';

    rAF = requestAnimationFrame(draw);
}

draw();</pre>

<h3 id="Pitfalls_4">Pitfalls</h3>

<ul>
 <li>You can't choose a specific framerate with <code>requestAnimationFrame()</code>. If you need to run your animation at a slower framerate, you'll need to use <code>setInterval()</code> or recursive <code>setTimeout()</code>.</li>
</ul>

<h3 id="Browser_compatibility_4">Browser compatibility</h3>

<p>{{Compat("api.Window.requestAnimationFrame")}}</p>

<h3 id="Further_information_4">Further information</h3>

<ul>
 <li><a href="/en-US/docs/Learn/JavaScript/Asynchronous/Timeouts_and_intervals">Cooperative asynchronous JavaScript: Timeouts and intervals</a>, in particular <a href="/en-US/docs/Learn/JavaScript/Asynchronous/Timeouts_and_intervals#requestanimationframe()">requestAnimationFrame()</a></li>
 <li><a href="/en-US/docs/Web/API/window/requestAnimationFrame">requestAnimationFrame() reference</a></li>
</ul>

<h2 id="Promises">Promises</h2>

<p><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promises</a> are a JavaScript feature that allows you to run asynchronous operations and wait until it is definitely complete before running another operation based on its result. Promises are the backbone of modern asynchronous JavaScript.</p>

<table class="standard-table">
 <caption>Useful for...</caption>
 <thead>
  <tr>
   <th scope="col">Single delayed operation</th>
   <th scope="col">Repeating operation</th>
   <th scope="col">Multiple sequential operations</th>
   <th scope="col">Multiple simultaneous operations</th>
  </tr>
  <tr>
   <td>No</td>
   <td>No</td>
   <td>Yes</td>
   <td>See <code>Promise.all()</code>, below</td>
  </tr>
 </thead>
</table>

<h3 id="Code_example_5">Code example</h3>

<p>The following code fetches an image from the server and displays it inside an {{htmlelement("img")}} element; <a href="https://mdn.github.io/learning-area/javascript/asynchronous/promises/simple-fetch-chained.html">see it live also</a>, and see also <a href="https://github.com/mdn/learning-area/blob/master/javascript/asynchronous/promises/simple-fetch-chained.html">the source code</a>:</p>

<pre class="brush: js">fetch('coffee.jpg')
.then(response =&gt; response.blob())
.then(myBlob =&gt; {
  let objectURL = URL.createObjectURL(myBlob);
  let image = document.createElement('img');
  image.src = objectURL;
  document.body.appendChild(image);
})
.catch(e =&gt; {
  console.log('There has been a problem with your fetch operation: ' + e.message);
});</pre>

<h3 id="Pitfalls_5">Pitfalls</h3>

<p>Promise chains can be complex and hard to parse. If you nest a number of promises, you can end up with similar troubles to callback hell. For example:</p>

<pre class="brush: js">remotedb.allDocs({
  include_docs: true,
  attachments: true
}).then(function (result) {
  let docs = result.rows;
  docs.forEach(function(element) {
    localdb.put(element.doc).then(function(response) {
      alert("Pulled doc with id " + element.doc._id + " and added to local db.");
    }).catch(function (err) {
      if (err.name == 'conflict') {
        localdb.get(element.doc._id).then(function (resp) {
          localdb.remove(resp._id, resp._rev).then(function (resp) {
// et cetera...</pre>

<p>It is better to use the chaining power of promises to go with a flatter, easier to parse structure:</p>

<pre class="brush: js">remotedb.allDocs(...).then(function (resultOfAllDocs) {
  return localdb.put(...);
}).then(function (resultOfPut) {
  return localdb.get(...);
}).then(function (resultOfGet) {
  return localdb.put(...);
}).catch(function (err) {
  console.log(err);
});</pre>

<p>or even:</p>

<pre class="brush: js">remotedb.allDocs(...)
.then(resultOfAllDocs =&gt; {
  return localdb.put(...);
})
.then(resultOfPut =&gt; {
  return localdb.get(...);
})
.then(resultOfGet =&gt; {
  return localdb.put(...);
})
.catch(err =&gt; console.log(err));</pre>

<p>That covers a lot of the basics. For a much more complete treatment, see the excellent <a href="https://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html">We have a problem with promises</a>, by Nolan Lawson.</p>

<h3 id="Browser_compatibility_5">Browser compatibility</h3>

<p>{{Compat("javascript.builtins.Promise")}}</p>

<h3 id="Further_information_5">Further information</h3>

<ul>
 <li><a href="/en-US/docs/Learn/JavaScript/Asynchronous/Promises">Graceful asynchronous programming with Promises</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Guide/Using_promises">Using promises</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise reference</a></li>
</ul>

<h2 id="Promise.all">Promise.all()</h2>

<p>A JavaScript feature that allows you to wait for multiple promises to complete before then running a further operation based on the results of all the other promises.</p>

<table class="standard-table">
 <caption>Useful for...</caption>
 <thead>
  <tr>
   <th scope="col">Single delayed operation</th>
   <th scope="col">Repeating operation</th>
   <th scope="col">Multiple sequential operations</th>
   <th scope="col">Multiple simultaneous operations</th>
  </tr>
  <tr>
   <td>No</td>
   <td>No</td>
   <td>No</td>
   <td>Yes</td>
  </tr>
 </thead>
</table>

<h3 id="Code_example_6">Code example</h3>

<p>The following example fetches several resources from the server, and uses <code>Promise.all()</code> to wait for all of them to be available before then displaying all of them — <a href="https://mdn.github.io/learning-area/javascript/asynchronous/promises/promise-all.html">see it live</a>, and see the <a href="https://github.com/mdn/learning-area/blob/master/javascript/asynchronous/promises/promise-all.html">source code</a>:</p>

<pre class="brush: js">function fetchAndDecode(url, type) {
  // Returning the top level promise, so the result of the entire chain is returned out of the function
  return fetch(url).then(response =&gt; {
    // Depending on what type of file is being fetched, use the relevant function to decode its contents
    if(type === 'blob') {
      return response.blob();
    } else if(type === 'text') {
      return response.text();
    }
  })
  .catch(e =&gt; {
    console.log(`There has been a problem with your fetch operation for resource "${url}": ` + e.message);
  });
}

// Call the fetchAndDecode() method to fetch the images and the text, and store their promises in variables
let coffee = fetchAndDecode('coffee.jpg', 'blob');
let tea = fetchAndDecode('tea.jpg', 'blob');
let description = fetchAndDecode('description.txt', 'text');

// Use Promise.all() to run code only when all three function calls have resolved
Promise.all([coffee, tea, description]).then(values =&gt; {
  console.log(values);
  // Store each value returned from the promises in separate variables; create object URLs from the blobs
  let objectURL1 = URL.createObjectURL(values[0]);
  let objectURL2 = URL.createObjectURL(values[1]);
  let descText = values[2];

  // Display the images in &lt;img&gt; elements
  let image1 = document.createElement('img');
  let image2 = document.createElement('img');
  image1.src = objectURL1;
  image2.src = objectURL2;
  document.body.appendChild(image1);
  document.body.appendChild(image2);

  // Display the text in a paragraph
  let para = document.createElement('p');
  para.textContent = descText;
  document.body.appendChild(para);
});</pre>

<h3 id="Pitfalls_6">Pitfalls</h3>

<ul>
 <li>If a <code>Promise.all()</code> rejects, then one or more of the promises you are feeding into it inside its array parameter must be rejecting, or might not be returning promises at all. You need to check each one to see what they returned. </li>
</ul>

<h3 id="Browser_compatibility_6">Browser compatibility</h3>

<p>{{Compat("javascript.builtins.Promise.all")}}</p>

<h3 id="Further_information_6">Further information</h3>

<ul>
 <li><a href="/en-US/docs/Learn/JavaScript/Asynchronous/Promises#running_code_in_response_to_multiple_promises_fulfilling">Running code in response to multiple promises fulfilling</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all">Promise.all() reference</a></li>
</ul>

<h2 id="Asyncawait">Async/await</h2>

<p>Syntactic sugar built on top of promises that allows you to run asynchronous operations using syntax that's more like writing synchronous callback code.</p>

<table class="standard-table">
 <caption>Useful for...</caption>
 <thead>
  <tr>
   <th scope="col">Single delayed operation</th>
   <th scope="col">Repeating operation</th>
   <th scope="col">Multiple sequential operations</th>
   <th scope="col">Multiple simultaneous operations</th>
  </tr>
  <tr>
   <td>No</td>
   <td>No</td>
   <td>Yes</td>
   <td>Yes (in combination with <code>Promise.all()</code>)</td>
  </tr>
 </thead>
</table>

<h3 id="Code_example_7">Code example</h3>

<p>The following example is a refactor of the simple promise example we saw earlier that fetches and displays an image, written using async/await (<a href="https://mdn.github.io/learning-area/javascript/asynchronous/async-await/simple-refactored-fetch.html">see it live</a>, and see the <a href="https://github.com/mdn/learning-area/blob/master/javascript/asynchronous/async-await/simple-refactored-fetch.html">source code</a>):</p>

<pre class="brush: js">async function myFetch() {
  let response = await fetch('coffee.jpg');
  let myBlob = await response.blob();

  let objectURL = URL.createObjectURL(myBlob);
  let image = document.createElement('img');
  image.src = objectURL;
  document.body.appendChild(image);
}

myFetch();</pre>

<h3 id="Pitfalls_7">Pitfalls</h3>

<ul>
 <li>You can't use the <code>await</code> operator inside a non-<code>async</code> function, or in the top level context of your code. This can sometimes result in an extra function wrapper needing to be created, which can be slightly frustrating in some circumstances. But it is worth it most of the time.</li>
 <li>Browser support for async/await is not as good as that for promises. If you want to use async/await but are concerned about older browser support, you could consider using the <a href="https://babeljs.io/">BabelJS</a> library — this allows you to write your applications using the latest JavaScript and let Babel figure out what changes if any are needed for your user’s browsers.</li>
</ul>

<h3 id="Browser_compatibility_7">Browser compatibility</h3>

<p>{{Compat("javascript.statements.async_function")}}</p>

<h3 id="Further_information_7">Further information</h3>

<ul>
 <li><a href="/en-US/docs/Learn/JavaScript/Asynchronous/Async_await">Making asynchronous programming easier with async and await</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Statements/async_function">Async function reference</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/await">Await operator reference</a></li>
</ul>

<p>{{PreviousMenu("Learn/JavaScript/Asynchronous/Async_await", "Learn/JavaScript/Asynchronous")}}</p>

<h2 id="In_this_module">In this module</h2>

<ul>
 <li><a href="/en-US/docs/Learn/JavaScript/Asynchronous/Concepts">General asynchronous programming concepts</a></li>
 <li><a href="/en-US/docs/Learn/JavaScript/Asynchronous/Introducing">Introducing asynchronous JavaScript</a></li>
 <li><a href="/en-US/docs/Learn/JavaScript/Asynchronous/Timeouts_and_intervals">Cooperative asynchronous JavaScript: Timeouts and intervals</a></li>
 <li><a href="/en-US/docs/Learn/JavaScript/Asynchronous/Promises">Graceful asynchronous programming with Promises</a></li>
 <li><a href="/en-US/docs/Learn/JavaScript/Asynchronous/Async_await">Making asynchronous programming easier with async and await</a></li>
 <li><a href="/en-US/docs/Learn/JavaScript/Asynchronous/Choosing_the_right_approach">Choosing the right approach</a></li>
</ul>