Back

Fetch

a minimal example

I have a json string inside a file data.json that goes like this : [{"a":5,"b":4},{"a":3,"b":9},{"a":2,"b":2}].

To retrieve it, on the js side :

let data = fetch('data.json')
 .then(r => r.json())

fetch json

fetch text

fetch a blob (for an image)

// all 3 function definitions do the same thing
let data = fetch(location_of_resource)
 .then(r => r.json())
 .then(arr_of_objs => {});

using promise.all()

let a = fetch('a.json').then(r => r.json());
let b = fetch('b.json').then(r => r.json());
let c = fetch('c.json').then(r => r.json());
let d = fetch('d.json').then(r => r.json());

Promise.all([a, b, c, d]).then((r) => {
  console.log(r);
});