【发布时间】:2017-02-22 23:49:11
【问题描述】:
进行预检查以查看 api 中的图像是否确实存在。我使用 'q' 作为我的承诺库和 'request' 库来发出 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(),我希望函数在所有承诺都得到满足后返回。
【问题讨论】:
标签: javascript node.js asynchronous request q