Lets use some promises:
fetchyes or no from this api:https://yesno.wtf/api. log out the answer- Get your battery level and if it is chargin ot not using
navigator.getBattery()
- Create a promise that resolves with a message after 4 seconds. Use this promise by logging out 'hello' after 4 seconds
- Create a promise that resolves with a message after 2 seconds.
- Use the promise: When the promise is resolved alert the message
- Now make the promise fail by rejecting it with an error message instead of resolving it, and log the error message to the console
Create a new function that returns a promise. This function should have one parameter: successMessage.
The promise should resolve after 1 second. When you resolve the promise, it should be resolved with the successMessage parameter.
Call the function with an argument, assign the return of the function to a variable (what type will that have?). Use the variable to log out the successMessage.
Now add the possibility for the promise to also reject. Give the function a new parameter: errorMessage. When the promise is rejected it should be rejected with the errorMessage parameter.
Now reject the promise after 1 second instead of resolving it.
give the function a third parameter: shouldReject. If shouldReject is true the promise being returned from the function should reject (with the errorMessage parameter) the promise. Otherwise the promise should resolve (with the successMessage parameter)
Sometimes you will be dependent on one API endpoint to call another API endpoint.
Go back to the fetch to the yesno api made in Exercise 1.
Use the answer to make a fetch to https://api.adviceslip.com/advice/search/{query}, replacing query with the answer.