Recursive Tree Walk with ES6 Promise

1.4k views Asked by At

I'm looking to walk an object tree of unknown depth and return a given node via an ES6 promise. (used lodash here, obviously not necessary, I realize). I've got the tree walking working fine but I'm a bit unclear the proper method to ensure that the top-level scope variable promise is passed into the recursive function calls so that it's available when calling .resolve( data ). Right now it attempts to execute on a successful find but fails to resolve the promise since the recursive function has overwritten with a new promise and it fails to bubble up the chain:

  deepFindReturn (object, searchKey, searchVal, cb) {
    if ( !cb ) cb = _.noop;
    for(let x in object){
      if (object.hasOwnProperty(x)) {
        if ( object[searchKey] === searchVal ) {
          return cb( object );
        }
        if ( _.isArray( object[x] ) && !this.found ){
          object[x].forEach( (item) => {
            this.deepFindReturn(item, searchKey, searchVal, cb);
          });
        } else if ( typeof object[x] === typeof {}  && !this.found ) {
          this.deepFindReturn(object[x], searchKey, searchVal, cb);
        }
      }
    }
  }

  deepFindReturn(data, 'uuid', 'f8d1ffed-9b51-4982-97b7-60f8e074eda4')

here's the tree I'm walking, in case it helps:

  data = {
     "name":"root",
     "created":"2015-04-07T20:36:29.711Z",
     "createdBy":"admin",
     "uuid":"9731cedc-8ed7-4367-b95d-30898c7913a1",
     "primaryType":"folder",
     "path":"/root",
     "itemCount":7,
     "items":[
        {
           "name":"219760_964977275754_1803638_o.jpg",
           "baseVersion":"afe1994e-d9a8-47fd-a7db-dcf0e085fc05",
           "created":"2015-06-01T13:30:16.490Z",
           "lastModified":"2015-06-01T13:30:16.490Z",
           "isCheckedOut":true,
           "createdBy":"admin",
           "versionHistory":"152eb76a-e0ac-446d-a7e8-47b5e5c821ed",
           "primaryType":"file",
           "lastModifiedBy":"admin",
           "uuid":"25cc6435-6432-47f3-8dc3-f94f2788f2ef",
           "parent":"9731cedc-8ed7-4367-b95d-30898c7913a1",
           "mimeType":"image/jpeg",
           "size":116285
        },
        {
           "name":"Child1",
           "created":"2015-04-07T21:03:41.729Z",
           "createdBy":"admin",
           "primaryType":"folder",
           "uuid":"f8d1ffed-9b51-4982-97b7-60f8e074eda4",
           "parent":"9731cedc-8ed7-4367-b95d-30898c7913a1",
           "itemCount":36
        },
        {
           "name":"Child2",
           "created":"2015-04-07T21:14:47.950Z",
           "createdBy":"admin",
           "uuid":"8f1246ff-5053-411a-88de-c465027b998d",
           "primaryType":"folder",
           "parent":"9731cedc-8ed7-4367-b95d-30898c7913a1",
           "itemCount":3
        },
        {
           "name":"Child3",
           "created":"2015-05-01T00:46:36.973Z",
           "createdBy":"admin",
           "uuid":"54f897a4-ac16-4585-83cb-d0e67ca73a74",
           "primaryType":"folder",
           "parent":"9731cedc-8ed7-4367-b95d-30898c7913a1",
           "itemCount":1
        },
        {
           "name":"Child4",
           "created":"2015-05-26T18:18:33.159Z",
           "createdBy":"admin",
           "primaryType":"folder",
           "uuid":"ad1344a9-08b7-44bb-b47d-0efb99c59ac3",
           "parent":"9731cedc-8ed7-4367-b95d-30898c7913a1",
           "itemCount":0
        },
        {
           "name":"Child5",
           "created":"2015-06-07T03:57:20.494Z",
           "createdBy":"admin",
           "primaryType":"folder",
           "uuid":"2b46d7e4-b50e-4eec-b97a-c46b2016926c",
           "parent":"9731cedc-8ed7-4367-b95d-30898c7913a1",
           "itemCount":0
        },
        {
           "name":"content.jpg",
           "baseVersion":"620c8448-3e51-4a27-b630-60c1272c19da",
           "created":"2015-06-03T15:09:25.192Z",
           "lastModified":"2015-06-03T15:09:25.193Z",
           "isCheckedOut":true,
           "createdBy":"admin",
           "versionHistory":"871afa2a-5a2c-4762-bc26-a69022234850",
           "primaryType":"file",
           "lastModifiedBy":"admin",
           "uuid":"c8c63420-b525-4b36-bce3-a7c4cc55c07a",
           "parent":"9731cedc-8ed7-4367-b95d-30898c7913a1",
           "mimeType":"image/jpeg",
           "size":30711
        }
     ]
  }
1

There are 1 answers

0
kim3er On BEST ANSWER

I'd move the bulk of the logic into another function and wrap that function in a Promise. I'm not sure the is any benefit to recursively creating promises for each branch of the tree. The function is non-blocking, with only minimal changes to logic.

function deepFindReturn (object, searchKey, searchVal) {
  function doer(object, searchKey, searchVal) {
    if ( object[searchKey] === searchVal ) {
      return object;
    }

    for(let x in object) {
      let val = object[x];

      if (typeof val === typeof searchVal) {
        continue;
      }

      if ( Array.isArray( val ) ) {
        for (let item of val) {
          let o = doer(item, searchKey, searchVal);
          if (o) {
            return o;
          }
        }
      }
      else if ( typeof val === 'object' ) {
        let o = doer(val, searchKey, searchVal);
        if (o) {
          return o;
        }
      }
    }
  };

  return new Promise(function(resolve) {
    resolve(doer(object, searchKey, searchVal));
  });
}

deepFindReturn(data, 'uuid', 'f8d1ffed-9b51-4982-97b7-60f8e074eda4')
  .then((o) => { console.log(o); });

Live Demo

If however, the point of the exercise was to experiment with recursive asynchronous code, I'd have a look at Async instead of promises. Much more usable in recursive situations.

The only way I think you can do it with promises, would be to generate a Promise for each iteration and add them to a new array. You'd pass that new array to Promise.race. Something like:

Promise.race(val.map(function(item) {
  return new Promise(function(resolve) {
    // Determine result
    resolve(result);
  });
}))
  .then((result) => { /* Deal with result */ });