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?