Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions Overview.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<p><a class="logo" href="https://whatwg.org/"><img alt="WHATWG" height="100" src="https://resources.whatwg.org/logo-fetch.svg" width="100"></a>
<h1 id="cors">Fetch</h1>
<h2 class="no-num no-toc" id="living-standard-—-last-updated-6-october-2015">Living Standard — Last Updated 6 October 2015</h2>
<h2 class="no-num no-toc" id="living-standard-—-last-updated-13-october-2015">Living Standard — Last Updated 13 October 2015</h2>

<dl>
<dt>Participate:
Expand Down Expand Up @@ -3110,7 +3110,7 @@ <h2 id="fetch-api"><span class="secno">6 </span>Fetch API</h2>
<p class="no-backref">The <a href="#dom-global-fetch"><code title="dom-global-fetch">fetch()</code></a> method is relatively
low-level API for <a href="#concept-fetch" title="concept-fetch">fetching</a> resources. It covers slightly
more ground than <a href="https://xhr.spec.whatwg.org/#xmlhttprequest"><code class="external" data-anolis-spec="xhr">XMLHttpRequest</code></a>, although it is
currently lacking when it comes to reporting progression.
currently lacking when it comes to request progression (not response progression).

<div class="example no-backref">
<p>The <a href="#dom-global-fetch"><code title="dom-global-fetch">fetch()</code></a> method makes it quite straightforward
Expand Down Expand Up @@ -3143,6 +3143,31 @@ <h2 id="fetch-api"><span class="secno">6 </span>Fetch API</h2>
params = {lat:35.696233, long:139.570431}
Object.keys(params).forEach(key =&gt; url.searchParams.append(key, params[key]))
fetch(url).then(/* … */)</pre>

<p>If you want to receive the body data progressively:

<pre>function consume(reader) {
var total = 0
return new Promise((resolve, reject) =&gt; {
function pump() {
reader.read().then(({done, value}) =&gt; {
if (done) {
resolve()
return
}
total += value.byteLength
log(`received ${value.byteLength} bytes (${total} bytes in total)`)
pump()
}).catch(reject)
}
pump()
})
}

fetch("/music/pk/altes-kamuffel.flac")
.then(res =&gt; consume(res.body.getReader()))
.then(() =&gt; log("consumed the entire body without keeping the whole thing in memory!"))
.catch(e =&gt; log("something went wrong: " + e))</pre>
</div>


Expand Down
27 changes: 26 additions & 1 deletion Overview.src.html
Original file line number Diff line number Diff line change
Expand Up @@ -3049,7 +3049,7 @@ <h2>Fetch API</h2>
<p class=no-backref>The <code title=dom-global-fetch>fetch()</code> method is relatively
low-level API for <span title=concept-fetch>fetching</span> resources. It covers slightly
more ground than <code data-anolis-spec=xhr>XMLHttpRequest</code>, although it is
currently lacking when it comes to reporting progression.
currently lacking when it comes to request progression (not response progression).

<div class="example no-backref">
<p>The <code title=dom-global-fetch>fetch()</code> method makes it quite straightforward
Expand Down Expand Up @@ -3082,6 +3082,31 @@ <h2>Fetch API</h2>
params = {lat:35.696233, long:139.570431}
Object.keys(params).forEach(key => url.searchParams.append(key, params[key]))
fetch(url).then(/* &hellip; */)</pre>

<p>If you want to receive the body data progressively:

<pre>function consume(reader) {
var total = 0
return new Promise((resolve, reject) => {
function pump() {
reader.read().then(({done, value}) => {
if (done) {
resolve()
return
}
total += value.byteLength
log(`received ${value.byteLength} bytes (${total} bytes in total)`)
pump()
}).catch(reject)
}
pump()
})
}

fetch("/music/pk/altes-kamuffel.flac")
.then(res => consume(res.body.getReader()))
.then(() => log("consumed the entire body without keeping the whole thing in memory!"))
.catch(e => log("something went wrong: " + e))</pre>
</div>


Expand Down