You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm all for the new named versions. We don't want to break compatibility with existing code by changing behaviour of existing functions, but new stuff could work. I'm not sure what the policy is yet on new interfaces that deviate from node.js though.
Where else does an interface like this make sense? Would this be a one-off of would there be a flood of requests to add the same style interface to other core APIs?
I think the way forward on this is for someone here to propose a change in a PR and then we'll escalate that to a TC discussion because it would be good to cover the question of whether adopting a new style of API is desirable. If it's too much work to come up with an initial implementation then we could just elevate this issue, it'd just be more of a hypothetical discussion then.
I think the streams version makes sense. I would test the iterator design in userland though. It can apply to any sort of stream, really. And it's easy enough to abstract a stream as an iterator in userland.
Iterables make no sense here; they are synchronous. (Generators are just iterables with .throw() and .return() methods; they don't make any sense either.)
There are vague designs by one TC39 committee member for adding RxJS-style observables to the language, along with for-on syntax. I doubt that will make the ES2016 train since no implementers have yet expressed interest, but I could be wrong. Regardless, it's very tentative. (There's another competing proposal, async iterables, that might be a better fit for this use case in particular.)
In the meantime, Node.js has an awkward version of observables/async-iterables/etc. already: object-mode streams. That's clearly the right choice for this situation, even if in the long run there becomes a language-level primitive for plural asynchronous values (similar to promises being the primitive for singular asynchronous values, or iterables being the primitive for plural synchronous values).
So I don't think this should be a new style of API. Just an object mode stream is fine.
Whoa, yeah this is a totally bogus suggestion as iterators and generators can't be used for iterating over async. This tripped me up at first as well.
But note that though it's not exactly what you had in mind fs.readFileSync(file, 'utf8') is already iterable by way of a String being iterable, and soon fs.readFileSync(file) will too be iterable as Buffers implement the iterable interface: #525
@Qard why should i use a iterator for a Sync call that will return after all is in mermory already but ok your right it could exist it can be done. i for my self would suggest for...of as iterate method but ok
Thank you, @frank-dspeed and @Qard, for responding. I'm sorry for not responding promptly. @frank-dspeed, I'm sorry, but I think you misunderstood me. What you suggested is what I meant, as it is the first part of what @jonathanong initially suggested. Ie, if we make directories sync iterable, then you could do something like this:
As for the second sentence of my initial comment on this issue, by "this", I meant "making directories sync iterable". Ie, I think a directory's default sync iterator's next() method could call dir.readSync() to get the directory's next entry.
@frank-dspeed It doesn't have to all be loaded into memory with a sync iterator. If you have a directory with millions of entries in it, a sync iterator could read and return entries one at a time, or in batches, but synchronously.
Activity
novacrazy commentedon Jan 24, 2015
Using generators/iterators in io.js where they make sense would be a good addition.
cjb commentedon Jan 24, 2015
Does this depend on the work in joyent/libuv#1574 to be merged first? I don't think it's in libuv yet.
vkurchatkin commentedon Jan 24, 2015
👍
in the future asynchronous version could return an observable:
hemanth commentedon Jan 24, 2015
👍
Qard commentedon Jan 24, 2015
I'm all for the new named versions. We don't want to break compatibility with existing code by changing behaviour of existing functions, but new stuff could work. I'm not sure what the policy is yet on new interfaces that deviate from node.js though.
@iojs/tc Thoughts?
rvagg commentedon Jan 26, 2015
Where else does an interface like this make sense? Would this be a one-off of would there be a flood of requests to add the same style interface to other core APIs?
jonathanong commentedon Jan 26, 2015
@rvagg I looked for any other APIs that would look like this but I couldn't find any. I think this may be a one-off.
rvagg commentedon Jan 26, 2015
I think the way forward on this is for someone here to propose a change in a PR and then we'll escalate that to a TC discussion because it would be good to cover the question of whether adopting a new style of API is desirable. If it's too much work to come up with an initial implementation then we could just elevate this issue, it'd just be more of a hypothetical discussion then.
Qard commentedon Jan 26, 2015
I think the streams version makes sense. I would test the iterator design in userland though. It can apply to any sort of stream, really. And it's easy enough to abstract a stream as an iterator in userland.
Fishrock123 commentedon Jan 26, 2015
The eventual observables thing sounds pretty rad.
jonathanong commentedon Jan 28, 2015
@Qard how would you create an iterator from a stream? streams are async... iterators are not...
@cjb i don't think it's merged. i'm going to open an issue in libuv now
Qard commentedon Jan 28, 2015
Just use co? (or something similar)
domenic commentedon Jan 28, 2015
OK, a few things:
.throw()and.return()methods; they don't make any sense either.)for-onsyntax. I doubt that will make the ES2016 train since no implementers have yet expressed interest, but I could be wrong. Regardless, it's very tentative. (There's another competing proposal, async iterables, that might be a better fit for this use case in particular.)So I don't think this should be a new style of API. Just an object mode stream is fine.
timoxley commentedon Jan 28, 2015
Whoa, yeah this is a totally bogus suggestion as iterators and generators can't be used for iterating over async. This tripped me up at first as well.
But note that though it's not exactly what you had in mind
fs.readFileSync(file, 'utf8')is already iterable by way of a String being iterable, and soonfs.readFileSync(file)will too be iterable as Buffers implement the iterable interface: #52598 remaining items
fs: introduce `opendir()` and `fs.Dir`
fs: introduce `opendir()` and `fs.Dir`
fs: introduce `opendir()` and `fs.Dir`
Fishrock123 commentedon Oct 12, 2019
This has been released in 12.12.0
ma11hew28 commentedon Nov 22, 2019
What about also making directories sync iterable (as initially suggested)?
I think this could be done by using
dir.readSync().The commit message could be "fs: make directories sync iterable".
frank-dspeed commentedon Nov 24, 2019
@ma11hew28 sorry to tell you that Sync can't be iterable as its Sync :) a iterable is a async type
Qard commentedon Nov 27, 2019
There are sync iterators too. It's entirely possible to make a sync version.
frank-dspeed commentedon Nov 27, 2019
@Qard why should i use a iterator for a Sync call that will return after all is in mermory already but ok your right it could exist it can be done. i for my self would suggest for...of as iterate method but ok
ma11hew28 commentedon Nov 27, 2019
Thank you, @frank-dspeed and @Qard, for responding. I'm sorry for not responding promptly. @frank-dspeed, I'm sorry, but I think you misunderstood me. What you suggested is what I meant, as it is the first part of what @jonathanong initially suggested. Ie, if we make directories sync iterable, then you could do something like this:
instead of something like this:
As for the second sentence of my initial comment on this issue, by "this", I meant "making directories sync iterable". Ie, I think a directory's default sync iterator's
next()method could calldir.readSync()to get the directory's next entry.Qard commentedon Nov 28, 2019
@frank-dspeed It doesn't have to all be loaded into memory with a sync iterator. If you have a directory with millions of entries in it, a sync iterator could read and return entries one at a time, or in batches, but synchronously.
fs: introduce `opendir()` and `fs.Dir`
fs: introduce `opendir()` and `fs.Dir`
unix,win: add uv_fs_{open,read,close}dir()
unix,win: add uv_fs_{open,read,close}dir()