0

I am getting the below result where I'm looping something.

Output

But I'm unable to get the [["PromiseValue"]] object.

Anyone please help me to do this.

Update :

Code I used for it.

function list(dir) {
 const walk = entry => {
 return new Promise((resolve, reject) => {
   fs.exists(entry, exists => {
     if (!exists) {
       return resolve({});
     }
     return resolve(new Promise((resolve, reject) => {
       fs.lstat(entry, (err, stats) => {
         if (err) {
           return reject(err);
         }
         if (!stats.isDirectory()) {
           return resolve({
             // path: entry,
             // type: 'file',
             name: path.basename(entry),
             time: stats.mtime,
             size: stats.size
           });
         }
         resolve(new Promise((resolve, reject) => {
           fs.readdir(entry, (err, files) => {
             if (err) {
               return reject(err);
             }
             Promise.all(files.map(child => walk(path.join(entry, child)))).then(children => {
               resolve({
                 // path: entry,
                 // type: 'folder',
                 name: path.basename(entry),
                 time: stats.mtime,
                 entries: children
               });
             }).catch(err => {
               reject(err);
             });
           });
         }));
       });
     }));
   });
 });
 }

  return walk(dir);
}

This is the code I used to convert the folder Structure to JSON Object.

But, this one gives the above result and I couldn't get the output form it.

Mari Selvan
  • 3,598
  • 3
  • 21
  • 36

2 Answers2

0

You can get the [["PromiseValue"]] by using a callback function

var promise1 = new Promise(function(resolve, reject) {
  setTimeout(resolve, 100, 'foo');
});


promise1.then(function(promiseVal){
  console.log('[[PromiseValue]]',promiseVal)
})

More details about Promise here

Kishore Kumar
  • 12,675
  • 27
  • 97
  • 154
0

Yes, I get the Answer.

First I store the value into temp1 in console(I know it does't matter at all).

And use the below code.

 var promise1 = Promise.resolve(temp1);
 promise1.then(function(value) {
     console.log(value);
 });

It gives the Below result what i want.

Result

Mari Selvan
  • 3,598
  • 3
  • 21
  • 36