JavaScriptでfetchを使う方法


動くコード
出典: https://sbfl.net/blog/2017/01/29/fetch-api/

fetch(url)
  .then((response) => response.text())
  .then((text) => console.log(text))
  .catch((error) => console.log(error));

動かないコード
出典: https://developer.mozilla.org/ja/docs/Web/API/Fetch_API/Using_Fetch

fetch('http://example.com/movies.json')
  .then(function(response) {
    return response.json();
  })
  .then(function(myJson) {
    console.log(JSON.stringify(myJson));
  });

なぜ本家のmozillaのコードが動かないのか謎。。。
追記:response.json()がExceptionを出していた模様。text()に変えると動きました。