javascriptasync-awaitasync-iterator

Async iterators and generators: Streaming GeoJSONL


I'm trying to replicate Brett Camper's code about streaming GeoJSONL files, and it is quite complex.

I'm trying to understand what it does step by step, but I really can't figure out what this syntax does:

streamGeoJSONL = async ƒ*(url)

Anyone can please explain it to me? If possible I would like to find something about it on Mozilla web docs.

Also, if someone could explain to me the flow of the code in the given example I would REALLY appreciate that!

Thanks!


Solution

  • Whenever you see the syntax async function*, it means the function is an AsyncGenerator. You can consume an AsyncGenerator by using for await...of on its generated AsyncIterator

    async function* streamGeoJSONL(url) {...} // AsyncGenerator
    
    const url = "https://s3.amazonaws.com/vtiles/honolulu_hawaii.geojsonl"
    
    streamGeoJSONL(url) // => AsyncIterator
    

    You get an AsyncIterator when you call an AsyncGenerator. That's what AsyncGenerator generates: an AsyncIterator

    You can consume an AsyncIterator with for await...of

    for await (const newFeatures of streamGeoJSONL(url)) {/* do stuff with newFeatures */}
    

    In the case of your example, newFeatures takes on the value that sent back by the keyword yield in streamGeoJSONL

    async function* streamGeoJSONL(url) {
      // ...
      yield lines.map(JSON.parse) // this is newFeatures
    } // AsyncGenerator