添加新参数检查后,我想对数组:skinydobazy 进行一些思考。
skinydobazy.forEach(function(klucz,index) {
getPrice(data.prices[0]).then(function(results) {
var price = results.skinData.wep.median_price;
if(price > 1)
klucz.check = 1;
console.log('HERE');
});
}); // end foreach
console.log(skinydobazy); //do sth after foreach
(foreach循环之后)我想用更新的skinydobazy做一些思考。但我有一个问题。因为首先显示skinydobazy,但应该“HERE”,之后应该显示skinydobazy。
有什么解决办法吗? 谢谢
最佳答案
通过使用map
您可以生成 Promise 数组,而不是 forEach
。然后,您可以使用 Promise.all
订阅该数组中所有 Promise 的完成情况。 .
Promise.all(skinydobazy.map(function(klucz, index) {
// Return the Promise that resolves on .then(fn)
return getPrice(data.prices[0]).then(function(results) {
var price = results.skinData.wep.median_price;
if (price > 1)
klucz.check = 1;
console.log('HERE');
// Resolve Promise with results
return results;
});
}).then(function(allResults) {
// allResults will be an array of results
console.log(skinydobazy);
});
关于javascript - javascript中如何执行同步性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33962332/