我正在使用節點從另一個網站請求應用程序詳細信息,但是我面臨的問題是它發送了數百(或者數千)的請求,然後出現錯誤,而我一無所獲。在幾百個承諾之間添加延遲
閱讀評論在信息代碼...
// I'm reading links from another file using 'fs'
fs.readFile('./google_apps/GAME_ACTION.json', 'utf8', function (err, data) {
if (err) throw err;
obj = JSON.parse(data);
// creating a empty array
var promiseStack = [];
for (var index in obj['GAME_ACTION']) {
var linksArray = obj['GAME_ACTION'][index];
linksArray.forEach(function(link, index) {
var appID = link.match(/id=.*/g)
var instr = appID.toString();
var appIDSliced = instr.slice(3)
// Here appIDSliced is ID, which is sliced from a link
// there are thousands on link in this file which I opened
console.log('sending') // for testing purpose
// here i'm pushing promises into that empty array
// store.app({id: appIDSliced}) is a promise I guess because I can use .then() on it...
// and store.app sends request to another website from which it receives an object in return.
// (Variable store is from another file 'require()' above in my node app fie)
promiseStack.push(store.app({id: appIDSliced}))
});
}
// After pushing all promises into array, now i'm trying to resolve them using Promise.all
Promise.all(promiseStack).then((responses) => {
console.log("Dealing with responses")
// Dealing with response (which is an object coming)
responses.map(response => {
var title = response.title
var package_name = response.appId
var appCategory = response.primaryGenre
var appSize = parseFloat((response.size/1024)/1024).toFixed(2)
var developerName = response.developer
var developerWebsite = response.developerWebsite
if (typeof developerWebsite == 'undefined') {
developerWebsite = "N/A"
}
var appPrice = response.price
var lastUpdated = response.updated
var contentRating = response.contentRating
if (typeof contentRating == 'undefined') {
contentRating = "N/A"
}
var userRating = response.score
if (typeof userRating == 'undefined') {
userRating = "N/A"
}
var dataRow = [appID, title, package_name, appCategory, appSize, developerName, developerWebsite, appPrice, lastUpdated, contentRating, userRating]
var dataToAdd = [dataRow];
console.log("Appending now")
// here i'm using google API to append that into my sheet on google
authentication.authenticate().then((auth)=>{
appendData(auth, dataToAdd);
});
})
})
})
見下圖......這些都是我的控制檯上我收到錯誤 保持它記錄「發送」像80秒,然後我得到錯誤「UnhandledPromiseRejectionWarning」,我剛剛停留了2分鐘,我一直按'CTRL + C'。
謝謝。
該錯誤告訴你,你錯過了一個* .catch * –
但我之前使用一個.catch(),並仍然得到類似這樣的請求TIMEDOUT(類似於),但我想添加延遲和擺脫這個錯誤.. – Tayyab
這不是推入陣列後。因爲循環中的異步程序不能信任數組中的所有內容。 'Promise.all'應該保存在'setTimeout(function(){},0);'中。接下來是嘗試'async.waterfall'就像一次執行n個數字,然後繼續下一步連接。我可以給你一個正常js的示例。相應地實施。 – Priya