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.