1
const params = {
    entity: 'musicTrack',
    term: 'Muzzy New Age',
    limit: 1
};

searchitunes(params).then(console.log);

I want searchitunes(params).then(console.log) to be a variable instead of being logged.

2 Answers2

1

Assuming that this follows the normal Javascript promises framework, then console.log is just a function passed into it like any other. So you can just use your own function where you access the response directly as a variable:

searchitunes(params).then(function(response) {
    //Your result is now in the response variable.
});

Or if you prefer the newer lambda syntax (the two are identical):

searchitunes(params).then(response => {
  //Your result is now in the response variable.
});

As per the comment, you can obtain the artwork URL by just traversing the object the same as you would any other object, so:

var artworkurl = response.results[0].artworkUrl100;

From there you can use AJAX to obtain the contents of that URL, or just create an img element that points to it.

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
  • I want to get the album art, here is a response form the api:https://pastebin.com/PTWzqLrN, I want the album art url to be a variable – Julian Sanchez May 02 '18 at 15:36
  • It worked but now I'm trying to use them and Its having an issue: see here https://stackoverflow.com/questions/50140444/issue-loading-img-to-html-from-json – Julian Sanchez May 02 '18 at 17:35
1

Just access it inside the then handler:

 searchitunes(params).then(result => {
   // Use result here
 });

Or using async / await:

 (async function() {
   const result = await searchitunes(params);
   // Use result here
 })();
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151