Your code has some problems with asychronous loading. And you are also going to append things out of order. Personally I would use fetch and promise all
function displayRestaurants() {
var table = document.getElementById("restaurantTable");
table.innerHTML = "";
var promises = restaurant_array.map(function(restaurant) {
return fetch('/averagepriceratings/' + restaurant._id)
.then(function(resp) {
return resp.json();
}).then(function(json) {
var average = json[0]['avg(priceRating)']
var logo = restaurant.logo;
var name = restaurant.name;
var tags = [restaurant.priceCategory, restaurant.cuisineCategory, restaurant.areaCategory].join(', ');
var cell = "<div>" + tags + "</div>";
return cell
});
});
Promise.all(promises).then(function(htmlStrs) => {
table.innerHTML = htmlStrs.join('');
});
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…