【发布时间】:2019-02-23 07:23:13
【问题描述】:
// TODO: FIX CODE - IT IS STILL NOT WAITING FOR populateVenueIDs
// BEFORE CALLING populateFsPhotoRequestURLs
// must use async, not sync to meet assignment requirements.
let populateVenueIDs = new Promise(function(resolve, reject) {
for (let y = 0; y < window.coffeeShopLocations().length; y++) {
let getVenueIDFromFS = new XMLHttpRequest();
getVenueIDFromFS.open('GET', window.fsURL[y]);
getVenueIDFromFS.onload = function() {
let responseFromFS = JSON.parse(getVenueIDFromFS.responseText);
window.fsVenueID[y] = responseFromFS.response.venues[0].id;
console.log(window.fsVenueID[y]);
};
getVenueIDFromFS.send();
}
resolve("done!");
});
function populateFsPhotoRequestURLs() {
for (let y = 0; y < window.coffeeShopLocations().length; y++) {
window.fsPhotoEndpoint[y] = 'https://api.foursquare.com/v2/venues/'
+ window.fsVenueID[y] + '/photos';
window.fsPhotoRequestURL[y] = fsPhotoEndpoint + '?' + fsPhotoParams;
console.log(window.fsPhotoRequestURL[y]);
}
}
populateVenueIDs.then(
populateFsPhotoRequestURLs()
);
在运行 populateFsPotoRequestURLs 之前,它仍然没有等待 populateVenueIDs。
我也尝试过 await、promise 和 then 的变体。我已经阅读了大约 15 个关于这些主题的教程,但没有一个足够接近我想要的。我不想使用超时(90% 的教程都使用),因为这会不必要地减慢应用程序的速度。我必须使用异步来满足分配要求。 请帮助我连接点以应用承诺和/或等待/然后在这种情况下适用。
【问题讨论】:
标签: javascript asynchronous promise async-await