预先检查api中的图像是否确实存在 . 我使用'q'作为我的promise库和'request'lib来发出http请求 . 我正在尝试返回所有成功请求的图像的数组,如下所示:
.then((data)=> {
return q.all(data.map((elem) => {
let deferred = q.defer()
request(`https://address/path/${elem}.jpg`, (error, response) => {
if (!error, response.statusCode === 200) {
deferred.resolve(elem)
}
})
return deferred.promise
})
)
})
.then((result)=> {
console.log('Array of existing images', result)//Result should contain all image id's which had status code 200
})
我希望这会返回所有请求状态为200的结果 . 对于 q.all()
,我希望在完成所有承诺后返回该函数 .
1 回答
我想,你shuld添加一个链,你清理非200图像例如,喜欢