Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save slebetman/972394a0e04e3bd71a6ac96441135ddc to your computer and use it in GitHub Desktop.
Save slebetman/972394a0e04e3bd71a6ac96441135ddc to your computer and use it in GitHub Desktop.
Using .then()
Promise.all([
prisma.portfolio.findUnique({where: { id: req.user.id }}),
prisma.portfolioStocks.findMany({where: { owner: { id: 1 }}})
]).then(result => {
let [portfolio, stocks] = result
let listOfStocks = stocks.map(x=>x.name)
let list=[...new Set(listOfStocks)] // generating list of unique stock names to retrieve price from api
let dataPromises = list.map((stock)=>{
return axios.get('https://data.messari.io/api/v1/assets/' + stock + '/metrics/market-data')
})
return Promise.all([stocks, Promise.all(dataPromises)]) // return an an array because we need to pass
// "stocks" to the next .then()
// Note: need to wrap the array in Promise.all()
// to resolve the inner promise.
}).then(result => {
let [stocks, data] = result
for (let j=0;j<data.length; j++) {
for(let i=0;i<stocks.length;i++){
if(stocks[i].name == data[j].data.data.Asset.name){
stocks[i].currentPrice = data[j].data.data.market_data.price_usd
}
}
}
res.send(stocks)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment