I can use the following function to convert an API to an object and print it out:
function covertApiTobj(){
fetch('https://api.chucknorris.io/jokes/random?category=dev')
.then(res => res.json())
.then(data => console.log(data));}
But how can I create another variable to let it equal to the object that has been converted from the API, something like this:
function covertApiTobj(){
fetch('https://api.chucknorris.io/jokes/random?category=dev')
.then(res => res.json())
.then(data => console.log(data));
const newdata = fetch('https://api.chucknorris.io/jokes/random?category=dev')
.then(res => res.json())
}
2
Answers
You can use promise chaining.
Here is an example:
You need to create
Promise
to pass a fetch response on a new variable andawait
the result inasync
function like this: