Created
August 8, 2023 23:01
-
-
Save ihorduchenko/cb50085fb5cb331e67d8f200429f00ec to your computer and use it in GitHub Desktop.
Handle 2 fetch API calls one by one
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var post; | |
// Call the API | |
fetch('https://jsonplaceholder.typicode.com/posts/5').then(function (response) { | |
if (response.ok) { | |
return response.json(); | |
} else { | |
return Promise.reject(response); | |
} | |
}).then(function (data) { | |
// Store the post data to a variable | |
post = data; | |
// Fetch another API | |
return fetch('https://jsonplaceholder.typicode.com/users/' + data.userId); | |
}).then(function (response) { | |
if (response.ok) { | |
return response.json(); | |
} else { | |
return Promise.reject(response); | |
} | |
}).then(function (userData) { | |
console.log(post, userData); | |
}).catch(function (error) { | |
console.warn(error); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment