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.

8
  • Show us usage code.
    – lolbas
    Feb 13, 2018 at 7:11
  • Its a promise, is it? Feb 13, 2018 at 7:11
  • yes this is promise Feb 13, 2018 at 7:12
  • I get the code From here stackoverflow.com/questions/11194287/… Feb 13, 2018 at 7:13
  • There is a lot of code there and no one will look through all the answers just to guess the code you are refering to. Post the code in the question.
    – lolbas
    Feb 13, 2018 at 7:14

2 Answers 2

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

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.