Search code examples
javascriptpromisechaining

How to pass a variable AND an API response to the next promise (.then)


I take advantage of the first promise "crypto.model.find()" to store an array of "symbols" ( symbol[] ) from the DB and get some IDs I will use to create an URL to make a request to an API >>> axios.get(url)

In the second promise, I receive the answer from the API but I don't have acccess to my array symbol[].

I need both at this stage and I don't know how to do it.

I read about returning an array and pass it down the promises chain but in this case, I don't think I can use such an array : return [ axios.get(url), symbol[] ]

// getting coins description in my DB with Mongoose  
cryptoModel.find()                                                      
.then ( docsCrypto => {
        let coingeckoIDsRequest = '';                                   
        let symbol = [];  

// loop on the response from the DB
        docsCrypto.forEach( (eachCrypto) => {                           
// Filling the array of symbols from the DB
            symbol.push( eachCrypto.symbol )                            
// creating a chunk of the HTTPS API URL request with the IDs from the DB
            coingeckoIDsRequest += eachCrypto.coingecko_id + '%2C'      
        })

// URL creation
        let url = 'https://api.coingecko.com/api/v3/coins/markets?vs_currency=eur&ids=' +
            coingeckoIDsRequest.substr(0, coingeckoIDsRequest.length-3) +
            '&order=market_cap_desc&per_page=100&page=1&sparkline=false&price_change_percentage=24h%2C7d%2C30d%2C200d'      

// returning the API data   
        return axios.get(url)                                           
})
// !!! >>>>  I want to get to the next line with the data from the API AND the array "symbol[]" 
.then (res => console.log(res)) 
// if error > console.log       
.catch (err => console.log(err))

Solution

  • Simply use Promise.all() to aggregate the two members you wish to pass on. It doesn't matter that one is a Promise and the other is Array.

    Judicious use of Array.prototype.map() avoids a lot of messing about.

    cryptoModel.find()
    .then(docsCrypto => {
        let symbols = docsCrypto.map(eachCrypto => eachCrypto.symbol);
        let url = 'https://api.coingecko.com/api/v3/coins/markets?vs_currency=eur&ids=' +
            docsCrypto.map(eachCrypto => eachCrypto.coingecko_id).join('%2C') +
            '&order=market_cap_desc&per_page=100&page=1&sparkline=false&price_change_percentage=24h%2C7d%2C30d%2C200d';
        return Promise.all([axios.get(url), symbols]);
    })
    

    In the next stage of the chain, destructuring provides a convenient way to access the two results.

    .then(([axiosResult, symbols]) => {
        console.log(axiosResult);
        console.log(symbols);
    });