You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can think of`async`as wrapping a function using`Promise.resolve()`.
1251
+
Você pode pensar em`async`como uma função encapsuladora usando`Promise.resolve()`.
1252
1252
1253
-
`await`can only be used in `async` functions:
1253
+
`await`pode ser usado apenas em funcões `async`:
1254
1254
1255
1255
```js
1256
1256
functionsleep() {
@@ -1268,6 +1268,10 @@ async function test() {
1268
1268
test()
1269
1269
```
1270
1270
1271
+
O código acime vai exibir `finish` antes de exibir `object`. Porque `await` espera pela funcão `sleep``resolve`, mesmo se a sincronização de código estiver seguida, ele não executa antes do código assíncrono ser executado.
1272
+
1273
+
A vantagem do `async` e `await` comparado ao uso direto da `Promise` mente em manipular a cadeia de chamada do `then`, que pode produzir código claro e acurado.
1274
+
1271
1275
The above code will print `finish` before printing `object`. Because `await` waits for the `sleep` function `resolve`, even if the synchronization code is followed, it is not executed before the asynchronous code is executed.
1272
1276
1273
1277
The advantage of `async` and `await` compared to the direct use of `Promise` lies in handling the call chain of `then`, which can produce clear and accurate code. The downside is that misuse of `await` can cause performance problems because `await` blocks the code. Perhaps the asynchronous code does not depend on the former, but it still needs to wait for the former to complete, causing the code to lose concurrency.
0 commit comments