Currently the fs-promise will use walk() method as any other and creates a Promise if it's called.
Unfortunately this is a special method of fs-extra and returns a Readable stream. The resolver will never call either the resolve() or the reject() method and the promise will hang forever because the nature of the stream demands to register listeners to events like 'data' or 'error'.
See example:
var fse = require('fs-extra');
var fsp = require('fs-promise');
const DIR = '/tmp';
var fseItems = [];
var fseWalking = fse.walk(DIR);
// copied from fs-extra documentation
fseWalking
.on('data', function (item) {
fseItems.push(item.path);
})
.on('end', function () {
// will print all items in the directory
console.dir(fseItems);
})
.on('error', function (error) {
// will print the error
console.log(error);
});
var fspWalking = fsp.walk(DIR);
fspWalking
.then(function () {
// never called
console.log('.then');
console.log(arguments.length);
console.log(arguments);
})
.catch(function () {
// never called
console.log('.catch');
console.log(arguments.length);
console.log(arguments);
});
console.log(fspWalking); // pending
With this commit the fs-promise will have a basic support for it and the example will execute as expected.
Because walk() returns the files one-by-one the wrapper method creates an array and will return the collection after the end event is dispatched. This needs to be supervised if the fs-extra package introduces a new method with stream support but right now I think this will do the trick.
This pull request adds support for the fs-extra's walk method.
Currently the fs-promise will use
walk()
method as any other and creates a Promise if it's called. Unfortunately this is a special method of fs-extra and returns a Readable stream. The resolver will never call either theresolve()
or thereject()
method and the promise will hang forever because the nature of the stream demands to register listeners to events like'data'
or'error'
.See example:
With this commit the
fs-promise
will have a basic support for it and the example will execute as expected.Because
walk()
returns the files one-by-one the wrapper method creates an array and will return the collection after the end event is dispatched. This needs to be supervised if the fs-extra package introduces a new method with stream support but right now I think this will do the trick.