Closed ehmicky closed 5 years ago
We're dropping callbacks in v3. They are very inefficient. We're using real streams, and it's very simple to construct any data structure with them; see readme examples.
Can't callbacks use the same logic as the streams under the hood? I.e. they would have the same performance.
For example when the user wants a callback or promise, the library could iterate over the stream, then resolve the callback or promise.
Some users might not be able to operate on results incrementally (e.g. if only need to know total number of items). For those users, using either for await
or a small utility function like get-stream
is an option, but a callback or promise would be simpler.
We are exposing readdir.promise
in v3, which should be enough for these cases:
const readdirp = require('readdirp').promise;
const entries = await readdirp('.');
Yes exactly that's perfect :+1:
allProcessed
is fired whenreaddirp
has fired allfileProcessed
callbacks. However somefileProcessed
callbacks might still be running if they are async (i.e. if they return a promise). It would be nice ifreaddirp
could allow users to wait for allfileProcessed
promises completion.At the moment users can achieve this with some extra code, by pushing promises to a shared array during
fileProcessed
, then wait for all promises onceallProcessed
has been fired. But it would be simpler ifreaddirp
could directly do this.A few suggestions on possible implementations (but any others will do):
fileProcessed
promises have resolvedFor information, this is what I have to do at the moment to achieve this: