0

I have a promise functin that resolve and pass an object to a second function called camparDir, the object is not passed successefully, where I did the mistake, the code is about getting a directory files than get for each file a timestamp of the creation date than I want to do a comparison of two directories, the problem is in the function of the comparDir where I don't receive the passed obj, and I want to iterate through it

Thank you in advance for your help

const getDirectories = function (src) {
    return new Promise(
        (resolve, reject) => {
          console.log(Date.now());
          glob(src+'/**/*',(err,res)=>{
            if(err){
              console.log('Error: ',err);
              reject(err);
            }else{
              serverTree = res;
              console.log('getDirectoreies Done')
              resolve(res)
            }
          });
        }
    );
  };

  const getTS = function(files){
    return new Promise(
      (resolve, reject) => {
        files.forEach(function(entry){
            fs.stat(entry, function(err, stats){
                serverTreeTS[entry] = Date.parse(stats.mtime);
            })
        })
        console.log('getTS done')
        let rrr = serverTreeTS
        resolve(rrr);
      }
    );
  };

  const comparDir = (trs) => {
    return new Promise(
      (resolve, reject) => {

        console.log(trs) /*
        the console shows this obj{}
          ./src/index.html: 1506183172000
          ./src/prom.js:    1505991591000
          ./src/starter.js: 1506255275000
          ./src/tree.json:  1506248976000
        */
        console.log(trs["./src/prom.js"]) // it shows: undefined

        Object.keys(trs).map(function(key, index) {
          console.log(key, index)         // nothing is logged
          console.log(trs[key])           // nothing is logged
        });

        for (var key in trs) {
          console.log(key, trs[key]); // nothing is logged
          if (trs.hasOwnProperty(key)) {
             console.log(key + ' -> ' + trs[key]); // nothing is logged
          }
       }
        resolve('done')
      }
    );
  };


  getDirectories('./src/').then(getTS).then(comparDir).then(function(){
    console.log(Date.now());
  }).catch(function (error) {
    console.log(error.message);
  });

NB: the log of trs in the console is done later as explained by T.J. Crowder, enter image description here

Ana Houa
  • 901
  • 1
  • 8
  • 19
  • 1
    Show that other function that you think passes a value into `comparDir`. Notice you most likely have the `new Promise` in the wrong place. The code you had posted is not asynchronous and should not use promises at all. – Bergi Sep 24 '17 at 12:33
  • Why do you use promises here? How do you call `comparDir` and what is `trs` exactly? – alexmac Sep 24 '17 at 12:33
  • As of when you do `console.log(trs)`, clearly `trs` does not have those properties in it. It does later, when you expand the console's view of the object, because the console has a live reference to the object and shows the properties as of when you *expand* it, not when it was logged. If you showed us how you were calling `comparDir`, we could probably help you understand why the object doesn't have its properties yet. – T.J. Crowder Sep 24 '17 at 12:34
  • T.J. Crowder I edited with the complete code, it just search for all files in a directory then it saves their creation timestamp than it is passed to comparDir where I want to compare a received obj with the created one (files with their timestamp) – Ana Houa Sep 24 '17 at 12:52
  • Bergi, What is the best way to use sync programming in javascript? I thought that promises will do the job – Ana Houa Sep 24 '17 at 13:11
  • I found the problem, It is with fs.stat which is async, I changed to fs.statSync and now all is fine, thank you T.J. Crowder for your comment about the console problem, I thought really that I am passing the obj correctly – Ana Houa Sep 24 '17 at 14:09

0 Answers0