Skip to main content
Minor edit - I think `console.error()` makes more sense than `console.log()` when catching an error. To see the difference, run the Stack Snippet at https://meta.stackoverflow.com/q/370046.
Source Link

Fetch returns a promise, and you can chain multiple promises, and use the result of the 1st request in the 2nd request, and so on.

This example uses the SpaceX API to get the info of the latest launch, find the rocket's id, and fetch the rocket's info.

const url = 'https://api.spacexdata.com/v4'; const result = fetch(`${url}/launches/latest`, { method: 'get' }) .then(response => response.json()) // pass the data as promise to next then block .then(data => { const rocketId = data.rocket; console.log(rocketId, '\n'); return fetch(`${url}/rockets/${rocketId}`); // make a 2nd request and return a promise }) .then(response => response.json()) .catch(errorerr => { console.logerror('Request failed', errorerr) }) // I'm using the result const to show that you can continue to extend the chain from the returned promise result.then(r => { console.log(r.first_stage); // 2nd request result first_stage property });
.as-console-wrapper { max-height: 100% !important; top: 0; }

Fetch returns a promise, and you can chain multiple promises, and use the result of the 1st request in the 2nd request, and so on.

This example uses the SpaceX API to get the info of the latest launch, find the rocket's id, and fetch the rocket's info.

const url = 'https://api.spacexdata.com/v4'; const result = fetch(`${url}/launches/latest`, { method: 'get' }) .then(response => response.json()) // pass the data as promise to next then block .then(data => { const rocketId = data.rocket; console.log(rocketId, '\n'); return fetch(`${url}/rockets/${rocketId}`); // make a 2nd request and return a promise }) .then(response => response.json()) .catch(error => { console.log('Request failed', error) }) // I'm using the result const to show that you can continue to extend the chain from the returned promise result.then(r => { console.log(r.first_stage); // 2nd request result first_stage property });
.as-console-wrapper { max-height: 100% !important; top: 0; }

Fetch returns a promise, and you can chain multiple promises, and use the result of the 1st request in the 2nd request, and so on.

This example uses the SpaceX API to get the info of the latest launch, find the rocket's id, and fetch the rocket's info.

const url = 'https://api.spacexdata.com/v4'; const result = fetch(`${url}/launches/latest`, { method: 'get' }) .then(response => response.json()) // pass the data as promise to next then block .then(data => { const rocketId = data.rocket; console.log(rocketId, '\n'); return fetch(`${url}/rockets/${rocketId}`); // make a 2nd request and return a promise }) .then(response => response.json()) .catch(err => { console.error('Request failed', err) }) // I'm using the result const to show that you can continue to extend the chain from the returned promise result.then(r => { console.log(r.first_stage); // 2nd request result first_stage property });
.as-console-wrapper { max-height: 100% !important; top: 0; }

added 33 characters in body
Source Link
Ori Drori
  • 195.7k
  • 32
  • 243
  • 233

Fetch returns a promise, and you can chain multiple promises, and use the result of the 1st request in the 2nd request, and so on.

This example uses the SpaceX API to get the info of the latest launch, find the rocket's id, and fetch the rocket's info.

const url = 'https://api.spacexdata.com/v4'; const result = fetch(`${url}/launches/latest`, { method: 'get' }) .then(response => response.json()) // pass the data as promise to next then block .then(data => { const rocketId = data.rocket; console.log(rocketId, '\n'); return fetch(`${url}/rockets/${rocketId}`); // make a 2nd request and return a promise }) .then(response => response.json()) .catch(error => { console.log('Request failed', error) }) // I'm using the result const to show that you can continue to extend the chain from the returned promise result.then(r => { console.log(r.first_stage); // 2nd request result first_stage property });
.as-console-wrapper { max-height: 100% !important; top: 0; }

Fetch returns a promise, and you can chain multiple promises, and use the result of the 1st request in the 2nd request, and so on.

This example uses the SpaceX API to get the info of the latest launch, find the rocket's id, and fetch the rocket's info.

const url = 'https://api.spacexdata.com/v4'; const result = fetch(`${url}/launches/latest`, { method: 'get' }) .then(response => response.json()) // pass the data as promise to next then block .then(data => { const rocketId = data.rocket; console.log(rocketId, '\n'); return fetch(`${url}/rockets/${rocketId}`); // make a 2nd request and return a promise }) .then(response => response.json()) .catch(error => { console.log('Request failed', error) }) // I'm using the result const to show that you can continue to extend the chain from the returned promise result.then(r => { console.log(r); // 2nd request result });
.as-console-wrapper { max-height: 100% !important; top: 0; }

Fetch returns a promise, and you can chain multiple promises, and use the result of the 1st request in the 2nd request, and so on.

This example uses the SpaceX API to get the info of the latest launch, find the rocket's id, and fetch the rocket's info.

const url = 'https://api.spacexdata.com/v4'; const result = fetch(`${url}/launches/latest`, { method: 'get' }) .then(response => response.json()) // pass the data as promise to next then block .then(data => { const rocketId = data.rocket; console.log(rocketId, '\n'); return fetch(`${url}/rockets/${rocketId}`); // make a 2nd request and return a promise }) .then(response => response.json()) .catch(error => { console.log('Request failed', error) }) // I'm using the result const to show that you can continue to extend the chain from the returned promise result.then(r => { console.log(r.first_stage); // 2nd request result first_stage property });
.as-console-wrapper { max-height: 100% !important; top: 0; }

deleted 10 characters in body
Source Link
Ori Drori
  • 195.7k
  • 32
  • 243
  • 233

Fetch returns a promise, and you can chain multiple promises, and use the result of the 1st request in the 2nd request, and so on.

This example uses the SpaceX API to get the info of the latest launch, find the rocket's id, and fetch the rocket's info.

varconst url = 'https://api.spacexdata.com/v4'; varconst result = fetch(`${url}/launches/latest`, {  method: 'get',  }) .then(function(response) {  return=> response.json();) // pass the data as promise to next then block }).then(function(data) => { varconst rocketId = data.rocket; console.log(rocketId, '\n'); return fetch(`${url}/rockets/${rocketId}`); // make a 2nd request and return a promise }) .then(function(response) {  return=> response.json(); }) .catch(function(error) => { console.log('Request failed', error) }) // I'm using the result variableconst to show that you can continue to extend the chain from the returned promise result.then(function(r) => { console.log(r); // 2nd request result });
.as-console-wrapper { max-height: 100% !important; top: 0; }

Fetch returns a promise, and you can chain multiple promises, and use the result of the 1st request in the 2nd request, and so on.

This example uses the SpaceX API to get the info of the latest launch, find the rocket's id, and fetch the rocket's info.

var url = 'https://api.spacexdata.com/v4'; var result = fetch(`${url}/launches/latest`, {  method: 'get',  }).then(function(response) {  return response.json(); // pass the data as promise to next then block }).then(function(data) { var rocketId = data.rocket; console.log(rocketId, '\n'); return fetch(`${url}/rockets/${rocketId}`); // make a 2nd request and return a promise }) .then(function(response) {  return response.json(); }) .catch(function(error) { console.log('Request failed', error) }) // I'm using the result variable to show that you can continue to extend the chain from the returned promise result.then(function(r) { console.log(r); // 2nd request result });
.as-console-wrapper { max-height: 100% !important; top: 0; }

Fetch returns a promise, and you can chain multiple promises, and use the result of the 1st request in the 2nd request, and so on.

This example uses the SpaceX API to get the info of the latest launch, find the rocket's id, and fetch the rocket's info.

const url = 'https://api.spacexdata.com/v4'; const result = fetch(`${url}/launches/latest`, { method: 'get' }) .then(response => response.json()) // pass the data as promise to next then block .then(data => { const rocketId = data.rocket; console.log(rocketId, '\n'); return fetch(`${url}/rockets/${rocketId}`); // make a 2nd request and return a promise }) .then(response => response.json()) .catch(error => { console.log('Request failed', error) }) // I'm using the result const to show that you can continue to extend the chain from the returned promise result.then(r => { console.log(r); // 2nd request result });
.as-console-wrapper { max-height: 100% !important; top: 0; }

deleted 10 characters in body
Source Link
Ori Drori
  • 195.7k
  • 32
  • 243
  • 233
Loading
added 12 characters in body
Source Link
Ori Drori
  • 195.7k
  • 32
  • 243
  • 233
Loading
corrected link on "chain promises" to include the 1st letter C.
Source Link
Catto
  • 6.5k
  • 2
  • 58
  • 58
Loading
deleted 182 characters in body
Source Link
Ori Drori
  • 195.7k
  • 32
  • 243
  • 233
Loading
edited body
Source Link
Ori Drori
  • 195.7k
  • 32
  • 243
  • 233
Loading
added 99 characters in body
Source Link
Ori Drori
  • 195.7k
  • 32
  • 243
  • 233
Loading
added 114 characters in body
Source Link
Ori Drori
  • 195.7k
  • 32
  • 243
  • 233
Loading
added 198 characters in body
Source Link
Ori Drori
  • 195.7k
  • 32
  • 243
  • 233
Loading
added 198 characters in body
Source Link
Ori Drori
  • 195.7k
  • 32
  • 243
  • 233
Loading
Source Link
Ori Drori
  • 195.7k
  • 32
  • 243
  • 233
Loading