Difference Between Symbol.iterator and Symbol.asyncIterator in JavaScript
Last Updated :
28 Jun, 2024
JavaScript offers powerful mechanisms for handling iteration through objects, especially with the introduction of symbols like Symbol.iterator
and Symbol.asyncIterator
. These symbols play important roles in defining how objects are iterated over, whether synchronously or asynchronously. In this article, we will explore the differences between Symbol.iterator
and Symbol.asyncIterator.
What is Symbol.iterator?
The Symbol.iterator is a well-known symbol in JavaScript used to make objects iterable. When an object implements this symbol as a method it becomes iterable meaning it can be used with the for...of loops, spread operators (...), and other constructs that expect iterables.
Characteristics:
- Synchronous Iteration: The Objects implementing Symbol.iterator provide the synchronous iteration over their elements.
- Iterable Protocol: The Objects define this symbol to conform to the iterable protocol allowing sequential access to their elements.
- Example Use: Arrays, maps, sets, and custom iterable objects often implement Symbol.iterator.
Applications:
- Iteration: The Allows iteration over collections using the standard iteration mechanisms.
- Custom Iterables: Enables custom objects to the support JavaScript's built-in iteration mechanisms seamlessly.
Example:
JavaScript
let myIterable = {
data: [1, 2, 3],
[Symbol.iterator]: function() {
let index = 0;
return {
next: () => ({
value: this.data[index],
done: index++ >= this.data.length
})
};
}
};
for (let item of myIterable) {
console.log(item);
}
Output:
1
2
3
What is Symbol.asyncIterator?
The Symbol.asyncIterator is a well-known symbol introduced in the ECMAScript 2018 (ES9) that defines the AsyncIterable interface. It allows the objects to be asynchronously iterable meaning they can be iterated over asynchronously using the for await...of loops.
Characteristics
- Asynchronous Iteration: The Objects implementing Symbol.asyncIterator provide the asynchronous iteration over their elements.
- AsyncIterable Protocol: The Objects define this symbol to conform to the AsyncIterable protocol allowing the asynchronous sequential access to their elements.
- Example Use: The Async generators, promises and custom asynchronous iterable objects use Symbol.asyncIterator.
Applications
- Async Iteration: This provides iteration over collections where elements are retrieved asynchronously.
- Concurrency: Useful in scenarios requiring asynchronous processing of the iterable data such as the handling streams or fetching data from APIs.
Example:
JavaScript
let asyncIterable = {
data: [1, 2, 3],
[Symbol.asyncIterator]: async function* () {
for (let i = 0; i < this.data.length; i++) {
await new Promise(resolve => setTimeout(resolve, 1000));
// Simulate async operation
yield this.data[i];
}
}
};
(async () => {
for await (let item of asyncIterable) {
console.log(item);
}
})();
Output:
1
2
3
Difference Between Symbol.iterator and Symbol.asyncIterator
Characteristics | Symbol.iterator | Symbol.asyncIterator |
---|
Type | Synchronous | Asynchronous |
---|
Iteration Type | The Synchronously iterates over elements | The Asynchronously iterates over elements |
---|
Protocol | Iterable Protocol | AsyncIterable Protocol |
---|
Mechanisms | for...of, Array.from(), spread operator (...) | for await...of |
---|
Example Usage | Arrays, maps, sets custom iterable objects | Async generators, promises and custom async iterables |
---|
Characteristics | The Provides synchronous iteration | The Provides asynchronous iteration |
---|
Applications | The Standard iteration mechanisms in JavaScript | The Asynchronous processing handling streams |
---|
Conclusion:
Understanding the difference between the Symbol.iterator and Symbol.asyncIterator is important for the implementing and working with the synchronous and asynchronous iteration in JavaScript. The Symbol.iterator is used for the synchronous iteration over objects while Symbol.asyncIterator enables asynchronous iteration accommodating scenarios requiring asynchronous data handling such as the network requests or streams.
Similar Reads
Difference between Generators and Iterators in JavaScript GeneratorsThe Generators are a special type of function in JavaScript that can be paused and resumed during their execution. They are defined using the asterisk (*) after the function keyword. The Generators use the yield keyword to yield control back to the caller while preserving their execution c
2 min read
What is the difference between await and yield keywords in JavaScript ? In this article, we will see how Await keyword is different from Yield keywords. Generator Functions: Functions that can return multiple values at different time interval as per the user demands, and can manage its internal state are generator functions. A function becomes a Generator function if it
3 min read
Difference between Anonymous and Named functions in JavaScript In JavaScript or in any programming language per say, functions, loops, mathematical operators and variables are the most widely used tools. This article will tell you about the difference between anonymous functions and named functions. We will discuss all the required concepts in this article to k
4 min read
Difference Between Object.assign and Spread Operator in JavaScript The Object.assign() and the Spread Operator (...) are commonly used for copying properties from one object to another. While they can achieve similar outcomes, they have distinct characteristics and use cases. This article will explain the differences between the Object.assign() and the spread opera
3 min read
Differences Between for-in and for-of Statement in JavaScript The for..in loop is designed for iterating over an object's keys or property names, making it useful when accessing each property in an object. Although it can also be used to iterate over the indices of an array. Conversely, the for..of loop is intended to iterate directly over values in iterable c
2 min read
Difference between forEach() and map() loop in JavaScript The forEach() and map() methods in JavaScript are used to iterate over arrays, but they serve different purposes. forEach() executes a provided function once for each array element without returning a new array, while map() transforms elements and returns a new array.JavaScript forEach() JavaScript'
4 min read