0

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)

1
  • 1
    did my answer helped you? Commented Oct 6, 2017 at 15:20

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.