Adding error handling to code example
This commit is contained in:
parent
5a073ce6b9
commit
ad9c9fd678
1 changed files with 6 additions and 3 deletions
|
@ -34,11 +34,14 @@ Your JavaScript could look like that:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
async function translate(idPatterns) {
|
async function translate(idPatterns) {
|
||||||
return fetch(document.location, {headers: {
|
const response = await fetch(document.location, {headers: {
|
||||||
'X-Translation-Request': idPatterns,
|
'X-Translation-Request': idPatterns,
|
||||||
'Accept-Language': document.documentElement.lang
|
'Accept-Language': document.documentElement.lang
|
||||||
}})
|
}});
|
||||||
.then(response => response.json());
|
if (! response.ok) {
|
||||||
|
return Promise.reject('Unexpected server response');
|
||||||
|
}
|
||||||
|
return await response.json();
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue