1
我正在使用一個抓取函數從數組中列出的一堆urls
中獲取一些數據。這裏有以下功能:以數組對象的形式返回錯誤並承諾
function getNbShares(urls) {
return Promise.map(urls, request).map((htmlOnePage, index) => {
const $ = cheerio.load(htmlOnePage),
share = $('.nb-shares').html();
return {
url: urls[index],
value: share
};
}).catch(function (urls, err) {
return {
url: urls[index],
value: err
};
});
}
它工作正常,但錯誤處理不是。我想要的是,當我有一個錯誤(無論是因爲頁面沒有加載,或者如果DOM選擇器是錯誤的),地圖函數/請求繼續做是工作,它返回錯誤(或空值)作爲一個值附加到最終數組對象中的url。
所有的URL從API獲取。我正在做這樣的事情'return getmyurls()。then((rows)=> {const urls = rows; return getNbShares(urls).then((nbshares)etc ...' –