Attempting to retrieve a value from a Promise and store it in a local variable within a function resulted in the Promise being resolved last.
The following function, which will be triggered in Vue.js mounted()
:
getPriceForYesterday(){
let yesterdayUSD = [];
for (let i = 0; i < this.cryptos.length; i++) {
let cryptoName = this.cryptos[i].title;
let timestamp = new Date(this.cryptos[i].purchaseDate);
timestamp.setDate(timestamp.getDate() - 1);
timestamp = timestamp.toISOString();
let priceYesterday = getPriceForTimestamp(cryptoName, timestamp);
Promise.all([priceYesterday]).then((values) => {
console.log("values", values)
yesterdayUSD.push(values[0]);
}).catch(e => console.error(e));
}
console.log("yesterdayUSD", yesterdayUSD);
console.log("yesterdayUSD[0]", yesterdayUSD[0]);
}
Result:
yesterdayUSD []
yesterdayUSD[0] undefined
values [13308.06]
values [1278.69]
The goal is to use yesterdayUSD
for comparison with a value stored locally and then return the comparison "result" to the vue data.