0

So I am going to preface this by saying that my understanding of callback functions is limited so there is a chance I am making a beginner mistake with either callbacks or RequireJS.

Essentially, what I am looking for is the ability to access the values of the method from an API and create a variable by looping through what is contained inside the callback function. I then want to take that variable and return its value in the return portion of my RequireJS define statement. Below is a simplified example of what I am trying to do.

//call an api library then loop through to get all values for your object
define(['apiLibrary'], function (api) {
    //create var that contains api method of current object
    var apiMethod = api.someMethod();
    //declare value var to be used inside callback
    var value ='';
    //call otherMethod, specifying an arguement and use callback to access contents of method
    apiMethod.otherMethod('arg',function (reply) {
        //loop through each value inside callback
        $.each(reply.item, function (key, value) {
            //add values to variable for each instance of method
            value += 'the key is '+key+' and the value is'+value;
        });
    });
    //return some values as well as the value set above for the overall define method
    return {
        valueFromElsewhere: 'hardcoded for example',
        valueFromLibrary: value //value is '' since it is set insde a callback function
    }
});

I appreciate any help I get in advance! Thanks!

EDIT: The information on promises is extremely helpful and definitely helps me wrap my head around asynchronous functions in general but I need to return my variable data in a RequireJS return statement. There is a downstream program, that I have no control over, expecting data to be returned in a specific format that is provided by the return value of my define function.

Kevin McGovern
  • 591
  • 1
  • 8
  • 25
  • One problem is that you have no idea when or even *if* the callback function will ever execute. What you should probably be looking at is how to incorporate promises into this workflow. See [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) – Seth Flowers Dec 16 '15 at 17:35
  • @KevinMcGovern There are only so many ways to pass values obtained asynchronously to code further down the stream. RequireJS changes nothing to this fact. If the code downstream cannot work with promises or callbacks, then you have to have "glue" code between your module and that other code. Again, this is not a RequireJS thing. – Louis Dec 16 '15 at 19:46

1 Answers1

1

You'll need to access the value asynchronously. A great way of handling that is a Promise:

var getValue = new Promise(function(resolve, reject) {
    apiMethod.otherMethod('arg',function (reply) {
        $.each(reply.item, function (key, value) {
            value += 'the key is '+key+' and the value is'+value;
        });
        resolve(value);
    });
});
return {
    valueFromElsewhere: 'hardcoded for example',
    getValueFromLibrary: getValue
};

You can then access it using the .then api:

foo.getValueFromLibrary.then(function(value) {
    console.log('Value:', value);
});
TbWill4321
  • 8,626
  • 3
  • 27
  • 25