[英]request-promise Unhandled rejection RequestError: Error: ETIMEDOUT
嗨我嘗試通過承諾請求寫一些下載功能,但如果我有超時我無法處理此錯誤,我嘗試meny示例但仍然有此錯誤
Unhandled rejection RequestError: Error: ETIMEDOUT
at new RequestError (/home/parse/node_modules/request-promise-core/lib/errors.js:14:15)
at Request.plumbing.callback (/home/parse/node_modules/request-promise-core/lib/plumbing.js:87:29)
at Request.RP$callback [as _callback] (/home/parse/node_modules/request-promise-core/lib/plumbing.js:46:31)
at self.callback (/home/parse/node_modules/request/request.js:186:22)
at emitOne (events.js:101:20)
at Request.emit (events.js:191:7)
at Timeout._onTimeout (/home/parse/node_modules/request/request.js:816:16)
at ontimeout (timers.js:380:14)
at tryOnTimeout (timers.js:244:5)
at Timer.listOnTimeout (timers.js:214:5)
我的代碼
下載功能:
function downloadPhoto(url,uploadUrl,name){
return new Promise(function(resolve, reject){
rp(url,{timeout:15000},function(e){if(e) reject(e);}).on('error', function(e){return reject(e);}).pipe(fs.createWriteStream(name+'.jpg')).on('finish', () => {
//console.log('done Download photo');
return resolve();
});
});
}
叫這個功能
function sndPht(url,uploadUrl){
return new Promise(function(resolve, reject){
return downloadPhoto(url,uploadUrl,name).then(function(){
..... some logic .....
}).catch(function(err){
return reject(err);
});
}
對於許多文件我在bluebird js map中調用函數:
Promise.map(photos, function(photo) {
if(photo.type === 'photo'){
return sndPht(photo,uploadUrl);
}
},{concurrency: 1});
我做錯了什么?
我有一個解決方案,如果你使用一個請求 - 承諾你大喊創建承諾並返回他並抓住它的例子,它不管用我的情況下的管道,所以我們需要改變功能下載像
function downloadPhoto(url){
var options = {
uri:url,
timeout:10000,
encoding: 'binary'
};
return rp(options);
}
然后我們可以像使用它一樣
return downloadPhoto(url).then(function(file){
fs.writeFileSync(name+'.jpg', file, 'binary');
}).catch(function(err){
console.log(err);
});
我們可以使用地圖
Promise.map(photos, function(photo) {
if(photo.type === 'photo'){
return sndPht(photo,uploadUrl);
}
},{concurrency: 1});
但如果你需要downlod大文件,你需要使用calback的請求
您可以使用Promise.race
來使用解析或拒絕的第一個promise的值。
使用這種技術,如果下載時間過長,我們可能會在一段時間后發生錯誤。 downloadPhoto
Promise仍將解決,但不會被處理
const images = [ { url: 'www.foo.com', uploadUrl: '/foo', name: 'foo' } , { url: 'www.bar.com', uploadUrl: '/bar', name: 'bar' } , { url: 'www.baz.com', uploadUrl: '/baz', name: 'baz' } ] const promiseTimeout = (delay, promise) => Promise.race([ new Promise((resolve, reject) => setTimeout(resolve, delay, { status: 'error', msg: 'took too long!' }) ), promise ]) const downloadPhoto = ({ url, uploadUrl, name }) => promiseTimeout( 1000, new Promise((resolve, reject) => { setTimeout(resolve, 3000, { status: 'success', msg: `this will never resolve ${url}` }) }) ) // map images array [...image] into [...Promise(image)] const imagePromises = images.map(downloadPhoto) // resolve all promises Promise.all(imagePromises) // called once all promises are resolved with array of results .then(images => { // map over the resolved images and do further processing images.map(console.log.bind(console, 'Image resolved')) }) // promises no longer reject, you will need to look at the status .catch(console.log.bind(console, 'Error: '))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.