
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
get-iterator
Advanced tools
Get the default iterator or async iterator for an iterable or async iterable
Get the default iterator or async iterator for an iterable or async iterable
Reduce the boilerplate of extracting the iterator from an object when you don't know if the object is an (async) iterable or already an (async) iterator.
npm install get-iterator
import { getIterator } from 'get-iterator'
const input = [1, 2, 3]
const it = getIterator(input)
console.log(it.next()) // { done: false, value: 1 }
console.log(it.next()) // { done: false, value: 2 }
console.log(it.next()) // { done: false, value: 3 }
console.log(it.next()) // { done: true, value: undefined }
Regular iterator from iterable:
import { getIterator } from 'get-iterator'
const input = [1, 2, 3]
const iterable = {
[Symbol.iterator] () {
let i = 0
return {
next () {
const value = input[i++]
return { done: !value, value }
}
}
}
}
const it = getIterator(input)
console.log(it.next()) // { done: false, value: 1 }
console.log(it.next()) // { done: false, value: 2 }
console.log(it.next()) // { done: false, value: 3 }
console.log(it.next()) // { done: true, value: undefined }
Async iterator from iterable:
import { getIterator } from 'get-iterator'
const input = [1, 2, 3]
const iterable = {
[Symbol.asyncIterator] () {
let i = 0
return {
async next () {
const value = await new Promise((resolve, reject) => {
setTimeout(() => resolve(input[i++]), 10)
})
return { done: !value, value }
}
}
}
}
const it = getIterator(iterable)
console.log(await it.next()) // { done: false, value: 1 }
console.log(await it.next()) // { done: false, value: 2 }
console.log(await it.next()) // { done: false, value: 3 }
console.log(await it.next()) // { done: true, value: undefined }
Already an iterator (probably):
import { getIterator } from 'get-iterator'
const input = [1, 2, 3]
let i = 0
const iterator = {
next () {
const value = input[i++]
return { done: !value, value }
}
}
const it = getIterator(iterator)
console.log(it.next()) // { done: false, value: 1 }
console.log(it.next()) // { done: false, value: 2 }
console.log(it.next()) // { done: false, value: 3 }
console.log(it.next()) // { done: true, value: undefined }
import { getIterator } from 'get-iterator'
getIterator(obj)
Get the default iterator or async iterator for an Iterable. If obj
is already an iterator (i.e. has a next
function) return it, since it's probably already an iterator.
This function will throw if obj
is not an iterable or iterator.
Name | Type | Description |
---|---|---|
obj | Iterable |Iterator | The object to extract the iterator from (may be an iterator already). |
Type | Description |
---|---|
Iterator | The result of calling obj[Symbol.iterator]() or obj[Symbol.asyncIterator]() or simply the passed obj if it is already an iterator. |
Feel free to dive in! Open an issue or submit PRs.
MIT © Alan Shaw
FAQs
Get the default iterator or async iterator for an iterable or async iterable
The npm package get-iterator receives a total of 156,477 weekly downloads. As such, get-iterator popularity was classified as popular.
We found that get-iterator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.