How can I get additional data from one promise to another?

Grav

I have one promise that relies on the return value of another promise. I'm successfully passing that value to the second promise, but there's additional data that's created in the first promise that I'd like to get into the second promise somehow.

Here's my code:

const getAlbumInfo = async (album, artist) => {
        let hostname = 'http://www.musicbrainz.org';
        let path = `/ws/2/release-group/?query=release:${album}%20AND%20artist:${artist}&fmt=json`;
        let res = await fetch(hostname + path);
        return res.json();
};


const getAlbumArt = async (albumInfo) => {
    let res = await fetch(`${albumInfo.url}`);
    return res.json();
};    


let artist = '', album = '';

getAlbumInfo(album, artist)
    .then((res) => {
        const metadata = {
            album: res['album'],
            artist: res['artist'],
            url: res['url']
        };

        return getAlbumArt(metadata);
    })
    .then((res) => {
        console.log(res);  // prints result from getAlbumArt

        // I also need the contents of metadata here
    })
    .catch(e => { console.error(e); });

So I'm getting album metadata in the first promise, which includes a URL, and then I'm running a fetch on that URL, which returns a second promise. The problem is that I need access to the album metadata in the second promise.

Since I'm returning/passing the metadata into the second promise, I tried using Object.assign() inside getAlbumArt() to combine the metadata and the fetch result, but that didn't seem to work.

Titus

There are multiple ways of doing this, you could use await:

const getAlbumInfo = async(album, artist) => {
  let hostname = 'http://www.musicbrainz.org';
  let path = `/ws/2/release-group/?query=release:${album}%20AND%20artist:${artist}&fmt=json`;
  let res = await fetch(hostname + path);
  return res.json();
};


const getAlbumArt = async(albumInfo) => {
  let res = await fetch(`${albumInfo.url}`);
  return res.json();
};


let artist = '',
  album = '';

const getAll = async() => {
  const res1 = await getAlbumInfo(album, artist);
  const metadata = {
    album: res['album'],
    artist: res['artist'],
    url: res['url']
  };
  const res2 = await getAlbumArt(metadata);

  // here you have access to both responses (res1 and res2) and the created metadata object.
}

If you use this, you should wrap the calls in try..catch...

Another option will be to pass the metadata from the second promise along with the response:

const getAlbumInfo = async(album, artist) => {
  let hostname = 'http://www.musicbrainz.org';
  let path = `/ws/2/release-group/?query=release:${album}%20AND%20artist:${artist}&fmt=json`;
  let res = await fetch(hostname + path);
  return res.json();
};


const getAlbumArt = async(albumInfo) => {
  let res = await fetch(`${albumInfo.url}`);
  res = await res.json();
  return {
    res,
    albumInfo
  };
};


let artist = '',
  album = '';

getAlbumInfo(album, artist)
  .then((res) => {
    const metadata = {
      album: res['album'],
      artist: res['artist'],
      url: res['url']
    };

    return getAlbumArt(metadata);
  })
  .then((o) => {
    console.log(o.res, o.albumInfo);
  })
  .catch(e => {
    console.error(e);
  });

And a third option will be to resolve the second promise inside the first promise's callback function:

getAlbumInfo(album, artist)
  .then((res) => {
    const metadata = {
      album: res['album'],
      artist: res['artist'],
      url: res['url']
    };

    getAlbumArt(metadata)
      .then(res2 => {
        // here you can access res, res2 and metadata
      })
      .catch(..);
  })
  .catch(..);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can i get data or pass a data from one page to another after submitting a form in reactjs

How can I get data from one container to another in docker? I am getting ECONNREFUSED error

How can I append data from one spreadsheet to another one?

How can I get a data from API promise?

how can I get data from a table that is liked to a table that is liked to another one it self?

How can I get a data from one html form and display it on another?

How can I transfer data from one route to another

How do I get data from one function to another

How do I get a data from one table to another but with calculations?

How can I get data from another function in onCreateView?

How can I get some data from another url in php?

How can I get data for one field from multiple tables?

How can I get distinct data from one col

How can I store the data I get from a Promise back into an object with the same key it came from?

How can I extract data from one pixel in an image and assign this data to another pixel in another image?

How can I get some additional information from an EF DbUpdateException

R: How can I transfer values from one data frame to another data frame depending on certain circumstances?

How i can get selection model from one method to another metod from other class with Table View?

How can I add columns of data from one table to another from a lookup ID number in CSV/Excel?

How can i pass data retrieved from json from one view to another view?

How can i get data from another model and show it in another resource using Laravel Filament

How can I pass data from one view to another in gluon mobile? how to switch to view and set Value?

How can I get multiple json data from json file one by one?

How i can get data from the diffrent pages and render that data into another page using shared preferences in flutter

How can I get the max value from one column where values match another column?

How can I get fields from one object and add them to another in Javascript?

How can I get the information from one field in a query to use in another query?

How can I get a change value event from one view to trigger a method in another

How can I get one value from a @client.command function and implement it into another