Почему показывается что массив пустой, хотя в нем есть элементы?
Даже в консоли показывается, что у него длина 5, но когда пишешь array.length, то выводиться 0
((global) => {
const timeout = 20;
const _async = (fn, cb) => {
setTimeout(() => {
cb(fn());
}, Math.random() * timeout);
};
const Folder = function(a = []) {
if (!new.target) {
return new Folder(a);
}
this.read = (index, cb) => _async(() => a[index], cb);
this.size = (cb) => _async(() => a.length, cb);
};
Object.freeze(Folder);
global.Folder = Folder;
});
const input = Folder([
'file',
'ffffile',
Folder([
'file',
]),
Folder([
'fiiile',
]),
Folder([{},
null,
'file',
'ffiillee',
'ffiillee',
]),
Folder([
Folder([
'filllle',
'file',
null,
]),
{},
Folder([])
]),
]);
async function solution(input, array = []) {
input.size(function(size) {
for (let i = 0; i < size; i++) {
input.read(i, function(file) {
if (typeof file === 'string') {
if (file != 'file') {
array.sort().push(file)
}
} else if (file != undefined && file != null) {
if (Object.keys(file).length != 0) {
solution(file, array)
}
}
})
}
})
return array
}
console.log(solution(input))
