0

I'm a little new to Javascript, and am having a hard time with the asynchronous aspect of it. My program checks values of two objects, where the second object doesn't have a vital property I need in order to complete the check. So I made a promise to get that value/property (the ID), and now I need to pass that ID value along to a check function. The check function should simply return a true/false to see if the ID's match. The value of the check function is passed to another function which then acts appropriately and edits the thing if necessary. So I basically can't access the value of tick outside it's brackets. I've included the snippet of my code where all of this is happening, as all of this is easier to visualize with it. Can someone provide me with a solution to this issue? Any advice would help immensely! I want to minimize the modification of the script as much as possible.

    var Q = require('q');

    getID = function(instance, person, callback){
          var = deferred = Q.defer();
          var url = 'www.blah.com'; 
          var options = {
              'url': url
          };

          request.get(options, function(error, response, body){
              if (error) {
                  deferred.reject(error);
              }else{
                  var res = body;
                  var obj = JSON.parse(res);
                  var id = obj.id;
                  deferred.resolve(id);
              } else deferred(obj);
          });


    check = function(instance, thing1, thing2){ 
        var tick = true;
        getID(instance, thing2).then(function(id)){
            var id_1 = thing1.id; // thing1 passed into check with ID
            var id_2 = thing2.id; // thing 2 now has id attached to it
            if( id_1 == id_2 ){
                tick = true; // VALUE 1 
            }else{
                tick = false; // VALUE 2
        });

        // NEED VALUE 1 OR 2 OF TICK HERE 

        if(thing1.name == thing2.name){
            tick = true;
        else{
            tick = false;
        }

        // similar checks to name but with ADDRESS, EMAIL, PHONE NUMBER
        // these properties are already appended to thing1 and thing 2 so no need to call for them

    };

    editThing = function(instance, thing, callback){

        var checked = check(instance, thing1, thing2);
        if(checked){
            // edit thing
        }else{
            // don't edit thing          
    };

2 Answers2

1

Since you're making a promise of work to be done, and you need output from that work, you'll need pass that promise along to the code who's wanting the final output.

I'm not going to try to rewrite the code from your post, so allow me to paraphrase:

getThing = function(thing){
  var deferred = Q.defer();
  ...
  request.get(options, function(error, response, body){
    if (error) {
      deferred.reject(error);
    } else {
      ...
      deferred.resolve(thingMadeFromResponse);
    }
  });
  return deferred;
}

check = function(thingWeHave, thingWeNeedFetched){ 
  return getThing(thingWeNeedFetched).then(function(thingWeFetched)){
    // check logic
    checked = thingWeHave.id == thingWeFetched.id;
    ...
    return checked;
  });
};

editThing = function(instance, thing, callback){
  check(thingWeHave, thingWeNeedFetched).then(function(checked) {
    if(checked){
        // edit thing
    }else{
        // don't edit thing
    }
  });
};
Will
  • 2,163
  • 1
  • 22
  • 22
  • for the function check, I need both thing1 and thing 2, does this solution support this? I tried it and the program isn't executing. – pokemongirl1234 Jul 20 '16 at 22:48
  • It's not entirely clear what you're expecting to already have on hand and what you're needing to fetch from blah.com, so it's hard to know what exactly you need passed around. I'll edit my answer with an assumption that you have one object already and are trying to compare it to data fetched from blah.com... – Will Jul 20 '16 at 22:58
  • So I have obj1 which is fine, but it is passed through editThing, to reach check function. I need to retieve id from getID, for obj2, and then pass BOTH objects through a series of checks – pokemongirl1234 Jul 20 '16 at 23:04
  • sorry for the confusion, it's easy to get intertwined within all of these details – pokemongirl1234 Jul 20 '16 at 23:05
  • WOW, the solution helped me understand promises and how to pass them so much more! You just made something I've been looking at for days so much simpler. Thanks so much – pokemongirl1234 Jul 20 '16 at 23:22
  • Is this the most simple solution? – pokemongirl1234 Jul 21 '16 at 14:37
-2

Promises

“thenable” is an object or function that defines a then method.

p.then(function(value) {
   // fulfillment
   console.log(value + ' is now available and passable via function argument');
  }, function(reason) {
  // rejection
});
Ronnie Royston
  • 16,778
  • 6
  • 77
  • 91