解码ReadableByteStreams

时间:2016-07-07 16:46:45

标签: javascript fetch

var url = 'http://www.googleapis.com/customsearch/v1?q=foo&searchType=image';

window.fetch(url)
  .then(decode)
  .catch(err => console.log(err));

function decode(r) {
  // r.body is a ReadableByteStream - how do I decode it to a string?
}

r.bodyReadableByteStream - 如何将其解码为字符串?

decode是正确的术语吗?

1 个答案:

答案 0 :(得分:1)

为此目的内置解码器:

window.fetch(url)
  .then(r => r.text())
  .catch(err => console.log(err));