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.

3

2 Answers 2

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
 })();
Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

I want to get the album art, here is a response form the api:pastebin.com/PTWzqLrN, I want the album art url to be a variable
It worked but now I'm trying to use them and Its having an issue: see here stackoverflow.com/questions/50140444/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.