Search code examples
javascriptnode.jsasynchronousecmascript-6es6-promise

Javascript Async: Rerun the function on map


Problem with map

I want to map the array, that reruns the function tree(...). However, it returns a array of promises which is not what i expected. I need it to be async.

What i expect to return is a array with this format:

{ name: '', path: '', type: '', ext: '', children: [ ... same array with all the files in the directory ] }

var Os = require('os')
var Path = require('path')
var Fs = require('fs')

const P = Path.join(Os.homedir(), 'Desktop', 'Devior', 'Fixture');

class Tree {
	constructor(path) {
		this.path = path
		this.tree(path)
	}
	async tree (path) {
		let name = Path.basename(path)
		let item = { path, name }
		let stats = await this.stats_safe()

		if(stats.isFile()) {
			let ext = Path.extname(path).toLowerCase()

			item.extension = ext
			item.type = 'file'
		}
		else if(stats.isDirectory()) {
			let data = await this.readdir_safe()

			item.children = data
				.map(child => this.tree(Path.join(path, child)))
				.filter(e => !!e)
			item.type = 'directory'
		}
		else { return null }
		return item
	}


	// Utils

	async stats_safe() {
		return new Promise((resolve, reject) => {
			Fs.stat(this.path, (err, stats) => {
				if(err) reject(err)
				resolve(stats)
			})
		})
	}
	async readdir_safe() {
		return new Promise((resolve, reject) => {
			Fs.readdir(this.path, (err, data) => {
				if(err && err.code !== 'EACCES')
					resolve(null)
				if(err) reject(err)
				resolve(data)
			})
		})
	}
}



new Tree(P)


Solution

  • Use Promise.all() to wait until all of those promisses will resolve, and then return array of values. Something like this should work:

    item.children = await Promise.all(data
        .map(async (child) => await this.tree(Path.join(path, child)))
    

    Also here is some info about how to use map and filter with arrays of promises. Worth to take a look