|
4 | 4 | <qhelp> |
5 | 5 | <overview> |
6 | 6 | <p> |
7 | | -In JavaScript, <code>async</code> functions always return a Promise object. |
8 | | -</p> |
9 | | - |
10 | | -<p> |
11 | | -PLACEHOLDER |
| 7 | +In JavaScript, <code>async</code> functions always return a promise object. |
| 8 | +To obtain the underlying value of the promise, the `await` operator or a call to `then` should be used. |
| 9 | +Attempting to use a Promise object instead of its underlying value can lead to expected behavior. |
12 | 10 | </p> |
13 | 11 |
|
14 | 12 | </overview> |
15 | 13 | <recommendation> |
16 | 14 |
|
17 | 15 | <p> |
18 | | -PLACEHOLDER |
| 16 | +Use the `await` operator to get the value contained in the promise. |
| 17 | +Alternatively, call `then` on the promise and use the value passed to the callback. |
19 | 18 | </p> |
20 | 19 |
|
21 | 20 | </recommendation> |
22 | 21 | <example> |
23 | 22 |
|
24 | 23 | <p> |
25 | | -PLACEHOLDER |
26 | | -</p> |
27 | | - |
28 | | -<sample src="examples/ImplicitOperandConversion.js" /> |
29 | | - |
30 | | -<p> |
31 | | -PLACEHOLDER |
32 | | -</p> |
33 | | - |
34 | | -<p> |
35 | | -PLACEHOLDER |
| 24 | +In the following example, the <code>getData</code> function returns a promise, |
| 25 | +and the caller checks if the returned promise is <code>null</code>: |
36 | 26 | </p> |
37 | 27 |
|
38 | | -<sample src="examples/ImplicitOperandConversionGood.js" /> |
| 28 | +<sample src="examples/MissingAwait.js" /> |
39 | 29 |
|
40 | 30 | <p> |
41 | | -PLACEHOLDER |
| 31 | +However, the null check does not work as expected. The <code>return null</code> statement |
| 32 | +on line 2 actually returns a <em>promise</em> containing the <code>null</code> value. |
| 33 | +Since the promise object itself is not equal to <code>null</code>, the error check is bypassed. |
42 | 34 | </p> |
43 | 35 |
|
44 | | -<sample src="examples/ImplicitOperandConversion2.js" /> |
45 | | - |
46 | 36 | <p> |
47 | | -PLACEHOLDER |
| 37 | +The issue can be corrected by inserting <code>await</code> before the promise: |
48 | 38 | </p> |
49 | 39 |
|
50 | | -<sample src="examples/ImplicitOperandConversion2Good.js" /> |
51 | | - |
52 | | -<p> |
53 | | -PLACEHOLDER |
54 | | -</p> |
55 | | - |
56 | | -<sample src="examples/ImplicitOperandConversion2Good2.js" /> |
| 40 | +<sample src="examples/MissingAwaitGood.js" /> |
57 | 41 |
|
58 | 42 | </example> |
59 | 43 | <references> |
60 | | - |
61 | | - |
62 | | -<li>Ecma International, <i>ECMAScript Language Definition</i>, 5.1 Edition, Section 9. ECMA, 2011.</li> |
63 | | - |
| 44 | +<li>MDN: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises">Using promises</a></li> |
| 45 | +<li>MDN: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function">Async functions</a></li> |
| 46 | +<li>MDN: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await">Await operator</a></li> |
64 | 47 | </references> |
65 | 48 | </qhelp> |
0 commit comments