Skip to content

Instantly share code, notes, and snippets.

@jakearchibald

jakearchibald/1.js Secret

Last active May 16, 2020
Embed
What would you like to do?
async function getResponseSize(url) {
const response = await fetch(url);
let total = 0;
// Here comes the new bit…
for await (const value of response) {
total += value.length;
console.log('Received chunk', value);
}
return total;
}
@stryju

This comment has been minimized.

Copy link

@stryju stryju commented Nov 5, 2016

shouldn't it be

for await (const value of reader.read())

just trying to grasp how would it know about what is "awaitable" in this for..of

@jakearchibald

This comment has been minimized.

Copy link
Owner Author

@jakearchibald jakearchibald commented Nov 6, 2016

@stryju with:

for (const item of collection)

collection[Symbol.iterator] is called to get an iterator for collection. With:

for await (const item of collection)

collection[Symbol.asyncIterator] is called to get an iterator that returns promises.

@dandv

This comment has been minimized.

Copy link

@dandv dandv commented Oct 11, 2018

I get TypeError: response is not async iterable.

@Coder2012

This comment has been minimized.

Copy link

@Coder2012 Coder2012 commented Jan 29, 2019

@dandv

response is a Promise and is not iterable, an array of them would be iterable however.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment