pokemongirl1234 pokemongirl1234 - 4 months ago 14
Javascript Question

How can I pass the value of a Promise to another Function?

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
};

Answer

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
    }
  });
};
Comments