Skip to content

Promises chaining #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions 1-js/11-async/03-promise-chaining/01-then-vs-catch/solution.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
The short answer is: **no, they are not the equal**:
A resposta breve é: **não, eles não são iguais**:

The difference is that if an error happens in `f1`, then it is handled by `.catch` here:
A diferença é que se um erro ocorrer em `f1` ele será tratado pelo `.catch` neste caso:

```js run
promise
.then(f1)
.catch(f2);
```

...But not here:
...Mas não neste:

```js run
promise
.then(f1, f2);
```

That's because an error is passed down the chain, and in the second code piece there's no chain below `f1`.
Isso é devido ao erro ser propagado pela cadeia, e no segundo código não há cadeia após `f1`.

In other words, `.then` passes results/errors to the next `.then/catch`. So in the first example, there's a `catch` below, and in the second one -- there isn't, so the error is unhandled.
Em outras palavras, `.then` passa resultados/erros para o próximo `.then/catch`. Então, no primeiro exemplo, há um `catch` em seguida, e no segundo exemplo não há, então o erro não é tratado.
4 changes: 2 additions & 2 deletions 1-js/11-async/03-promise-chaining/01-then-vs-catch/task.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Promise: then versus catch
# Promessa: then versus catch

Are these code fragments equal? In other words, do they behave the same way in any circumstances, for any handler functions?
Estes fragmentos de código são iguais? Em outras palavras, eles se comportam da mesma maneira, em quaisquer circunstâncias, para qualquer função tratadora?

```js
promise.then(f1).catch(f2);
Expand Down
196 changes: 88 additions & 108 deletions 1-js/11-async/03-promise-chaining/article.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion 1-js/11-async/03-promise-chaining/getMessage.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
function getMessage() {
return "Hello, world!";
return "Olá, mundo!";
}
21 changes: 1 addition & 20 deletions 1-js/11-async/03-promise-chaining/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,11 @@
script.src = src;

script.onload = () => resolve(script);
script.onerror = () => reject(new Error("Script load error: " + src));
script.onerror = () => reject(new Error("Erro ao carregar script: " + src));

document.head.append(script);
});
}

class HttpError extends Error {
constructor(response) {
super(`${response.status} for ${response.url}`);
this.name = 'HttpError';
this.response = response;
}
}

function loadJson(url) {
return fetch(url)
.then(response => {
if (response.status == 200) {
return response.json();
} else {
throw new HttpError(response);
}
})
}
</script>

<style>
Expand Down
59 changes: 1 addition & 58 deletions 1-js/11-async/03-promise-chaining/promise-handler-variants.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 1 addition & 38 deletions 1-js/11-async/03-promise-chaining/promise-then-chain.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 1 addition & 32 deletions 1-js/11-async/03-promise-chaining/promise-then-many.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.