Skip to content

Instantly share code, notes, and snippets.

@ihorduchenko
Created August 8, 2023 23:01
Show Gist options
  • Save ihorduchenko/cb50085fb5cb331e67d8f200429f00ec to your computer and use it in GitHub Desktop.
Save ihorduchenko/cb50085fb5cb331e67d8f200429f00ec to your computer and use it in GitHub Desktop.
Handle 2 fetch API calls one by one
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