Synchronous logging of asynchronous retrieved folder info I started learning functional programming with JavaScript and I have the basic idea of Functors, container types and etc. I was trying to experiment with arrays but I don't know how to solve the problem of a function call that iterates over a array and returns returns another array. So the first call ends up with an array with 4 elements which are nested nested arrays of promises.
const iterate = p =&gt;
Promise.all(p
.map(f=&gt;({localPath:f.localPath,RelativePathFromDistLibrary:f.RelativePathFromDistLibrary}))
.map(f=&gt;getsubfolders(f.localPath)
  .map(async _f=&gt;({
    RelativeDistLibraryUrlAfterDomain: env.spDistLibraryAbsoluteUrlArray.slice(1).join(''),
    RelativeDistFolder:`${env.spDistFolderAbsoluteUrlArray.slice(3).join('')}${f.RelativePathFromDistLibrary}${_f}`,
    ModifiedTimeInMs: (await readStat(`${f.localPath}${_f}`)).mtimeMs
  }))
))
.then(x=&gt;Promise.all(x.flat(Infinity)))
.then(x=&gt;x.map(z=&gt;console.log(z.ModifiedTimeInMs)))

The first call for iterate we pass an array of four objects. The first map just transforms each object, the second one gets all subfolders for each directory.
Example:
iterate([{RelativePathFromDistLibrary: '/src', localPath: './src'}]))

Then we map over all the results (first nested map). We wrap all in a promise.all since it will return a array of promises. We flatten the resulting array and wrap in a promise all again, then we can access the props.
Any tips that can help me better understand FP or solve the issues from this code would be super helpful.
I decided to go with the solution below.
By using Tasks I don't need to put all the logic right there, I can just pass the Task anywhere and map/chain over it to add extra functionality. I can even just fork it after I have all the data and get the List inside which I can also map and more.
Also with the function &quot;getsubfolders&quot; from the questions I can use to get the array of the paths and just wrap it in a List as I am doing in readDir.
So what I mean is that, although the structure is different, this way is way more flexible and readable (I can still derive all the above objects from the result).
I am noew trying to convert the getsubfolders to async and functional as well, for the sake of learning.
/* eslint-disable import/extensions */
/*
taken from 
https://mostly-adequate.gitbooks.io/mostly-adequate-guide/content/ch08.html#asynchronous-tasks
 */
import fs from 'fs'
import {Task, traverse,concat,compose,chain,map, prop, List, } from './shared/functional.js';

// readDir :: String -&gt; Task Error (List String)
const readDir = path =&gt; new Task((reject, result) =&gt; {
  fs.readdir(path, (err, data) =&gt; (err ? reject(err) : result(new List(data))));
});

// readDir :: String -&gt; Task Error (Stat)
const readStat = filename =&gt; new Task((reject, result) =&gt; {
  fs.stat(filename, (err, data) =&gt; (err ? reject(err) : result(data)));
});

const dirList = dir =&gt; 
chain(
  traverse(Task.of, compose(readStat, concat(dir))),
  readDir(dir),
);

dirList('./shared/').fork(console.error, map(compose(console.log, prop('mtimeMs'))));