Work through these in order.
Using async await
fetchyes or no from this api:https://yesno.wtf/api. Show the answer on the page.
Using promises
fetchyes or no from this api:https://yesno.wtf/api. Show the answer on the page.- Try fetching a url that rejects e.g.
https://knajskdskj.jasdk. Show the error message on the page.
- Create a promise that resolves after 4 seconds. Use this promise to show the text
helloon the page after 4 seconds. - Now make the promise fail by rejecting it with an error message instead of resolving it, and show the error message on the page.
Create a function that returns a promise, that you can use like this:
// YesNoFail4Seconds should wait 4 seconds before it does one of the following 3 things:
// resolves with a yes
// resolves with a no
// or rejects
// Look into Math.random()
YesNoFail4Seconds()
.then((data) => {
// Show on the page: The answer is ${data}
})
.catch((error) => {
// Show on the page: the error
});The above example show how to consume the promise using promises. Now try consume the YesNoFail4Seconds using async/await
Using async await
- Fetch a user from JSONPlaceholder (for example
https://jsonplaceholder.typicode.com/users/1) - After that succeeds, fetch movies using this api
- Show the movies on the page
Get the JSONPlaceholder user and the movies at the same time. Show the movies and the battery status on the page when the related promises have resolved.