Skip to content

Commit

Permalink
UPDATE
Browse files Browse the repository at this point in the history
  • Loading branch information
lksilva committed Sep 23, 2018
1 parent ca90e8d commit 8e7f04f
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions JS/JS-br.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
- [Debouncing](#debouncing)
- [Throttle](#throttle)
- [Map、FlatMap e Reduce](#mapflatmap-e-reduce)
- [Async e await](#async-and-await)
- [Async e await](#async-e-await)
- [Proxy](#proxy)
- [Por que 0.1 + 0.2 != 0.3](#why-01--02--03)
- [Expressões regulares](#regular-expressions)
Expand Down Expand Up @@ -1237,9 +1237,9 @@ function b() {
```
# Async and await
# Async e await
`async` function will return a `Promise`:
A função `async` vai retornar uma `Promise`:
```js
async function test() {
Expand All @@ -1248,9 +1248,9 @@ async function test() {
console.log(test()); // -> Promise {<resolved>: "1"}
```
You can think of `async` as wrapping a function using `Promise.resolve()`.
Você pode pensar em `async` como uma função encapsuladora usando `Promise.resolve()`.
`await` can only be used in `async` functions:
`await` pode ser usado apenas em funcões `async`:
```js
function sleep() {
Expand All @@ -1268,6 +1268,10 @@ async function test() {
test()
```
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.
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.
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.
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.
Expand Down

0 comments on commit 8e7f04f

Please sign in to comment.