Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
wnleao merged 2 commits intojavascript-tutorial:masterfromwnleao:master
May 16, 2020
Merged
Show file tree
Hide file tree
Changes fromall 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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.
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletions1-js/11-async/03-promise-chaining/article.md
View file
Open in desktop

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion1-js/11-async/03-promise-chaining/getMessage.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
function getMessage() {
return "Hello, world!";
return "Olá, mundo!";
}
21 changes: 1 addition & 20 deletions1-js/11-async/03-promise-chaining/head.html
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
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 deletions1-js/11-async/03-promise-chaining/promise-then-chain.svg
View file
Open in desktop
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 deletions1-js/11-async/03-promise-chaining/promise-then-many.svg
View file
Open in desktop
Loading
Sorry, something went wrong.Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

[8]ページ先頭

©2009-2025 Movatter.jp