proposal-async-iteration | Asynchronous iteration for JavaScript | Functional Programming library
kandi X-RAY | proposal-async-iteration Summary
kandi X-RAY | proposal-async-iteration Summary
The iterator interface (introduced in ECMAScript 2015) is a sequential data access protocol which enables the development of generic and composable data consumers and transformers. Their primary interface is a next() method which returns a { value, done } tuple, where done is a boolean indicating whether the end of the iterator has been reached, and value is the yielded value in the sequence. Since both the next value in the sequence and the "done" state of the data source must be known at the time that the iterator method returns, iterators are only suitable for representing synchronous data sources. While many data sources encountered by the JavaScript programmer are synchronous (such as in-memory lists and other data structures), many others are not. For instance, any data source which requires I/O access will be typically represented using an event-based or streaming asynchronous API. Unfortunately, iterators cannot be used to represent such data sources. (Even an iterator of promises is not sufficient, since that only allows asynchronous determination of the value, but requires synchronous determination of the "done" state.). In order to provide a generic data access protocol for asynchronous data sources, we introduce the AsyncIterator interface, an asynchronous iteration statement (for-await-of), and async generator functions.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of proposal-async-iteration
proposal-async-iteration Key Features
proposal-async-iteration Examples and Code Snippets
Community Discussions
Trending Discussions on proposal-async-iteration
QUESTION
Under the assumption that all the promises resolve, is asynchronous iteration (for-await-of
loop) faster than using Promise.all
?
From the specification on asynchronous iteration:
Each time we access the next value in the sequence, we implicitly
await
the promise returned from the iterator method.
Using asynchronous iteration:
...ANSWER
Answered 2018-Aug-19 at 11:05Is asynchronous iteration (for-await-of loop) faster than using Promise.all?
No. Both the loop and Promise.all will finish when the last promise resolves, which will be roughly at the same time. If the last promise that resolves is the first promise in the array, then Promise.all finishes immeadiately while the loop still has to iterate the other elements which might cause a small overhead but that should not matter. The only situation were it really matters is:
If one of the promises gets rejected, Promise.all exits immeadiately, while the loop has to reach that promise.
I'm wondering if asynchronous iteration starts looping before all the pages are finished fetching.
Yes you could get the first results a bit earlier, but all results will be available at the same time. Using the for loop would make sense if you want to show the data to the user, as he can start reading while the rest is still fetching, if you however want to accumulate the data it makes sense to await them all as it simplifies the iteration logic.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install proposal-async-iteration
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page