0

I am using this LastFM wrapper plugin (https://github.com/fxb/javascript-last.fm-api). What I want to do is return the result after the success function.

var getTopTracks = function(){
    var last_fm;

    last_fm = new LastFM({
       apiKey    : //apikey,
       apiSecret : //apiSecret
    });

    last_fm.user.getTopTracks({
        user   : //lastfm username
        period : //track period
        limit  : //track count
    }, {
       success: function( data ){
          var track_arr;
          track_arr = data.toptracks.track;

         // HOW DO I RETURN THE TRACKARRAY TO USE LATER??
       },
         error: function( code, message ){}
       });
};    


// I would like to do something like
// that would output the results (track_array) from the success function inside getTopTracks
var result = getTopTracks();

// Edit

Ultimately what I want to do is:

1) Loop over an array of usernames

2) Get the top tracks of each lastFM username (which is an array)

3) Combine all the items from each array into one new array

4) Output to DOM

var user_list = [
   // array of last fm usernames
];

var getTopTracks = function( user, callback ){
    var last_fm;

    last_fm = new LastFM({
       apiKey    : //apikey,
       apiSecret : //apiSecret
    });

    last_fm.user.getTopTracks({
        user   : user
        period : //track period
        limit  : //track count
    }, {
       success: function( data ){
          var track_arr;
          track_arr = data.toptracks.track;
                  callback( track_arr );

         // HOW DO I RETURN THE TRACKARRAY TO USE LATER??
       },
         error: function( code, message ){}
       });
}; 

for (var i = 0; i < user_list.length; i++) {
    getTopTracks(user_list[i], function( data ){
        console.log( data );
    });
} 

How can I loop through the username array, run the getTopTracks function for each user, and then combine the results into one new array AFTER ALL HAVE finished loading?

cusejuice
  • 10,285
  • 26
  • 90
  • 145

2 Answers2

0

Its an asynchronous call so it's not going to work like that. You need to utilize a callback function:

success: function( data ){
      var track_arr;
      track_arr = data.toptracks.track;

      callback(track_arr);
},

function callback(arr) {
    console.log(arr); //there it is, now do stuff!
}

Or, use an anonymous func:

var getTopTracks = function(callback) {

..
..

success: function( data ){
      var track_arr;
      track_arr = data.toptracks.track;

      callback(track_arr);
},

And now call it and pass in an anonymous func

getTopTracks(function(data) {
    console.log(data); //there it is again! do stuff!
});
tymeJV
  • 103,943
  • 14
  • 161
  • 157
0

Short answer is: You don't.

It's a callback, so any processing you want to do must be done within the callback, or the callback must call another callback, passing its result as an argument.

success: function( data ){
          var track_arr;
          track_arr = data.toptracks.track;

          doSomethingAwesomeWithResults(track_arr);
       },

And then, defined outside the API call:

function doSomethingAwesomeWithResults(track_arr) {
    //your custom logic here
}
Jonah
  • 15,806
  • 22
  • 87
  • 161