淡淡烟草味2017-05-24 11:40:32
可以使用promise
const p = Promise.race([
request('/resource-that-may-take-a-while'),//下载图片 改成真正的下载
new Promise(function (resolve, reject) {
setTimeout(() => reject(new Error('request timeout')), 3000)
})
]);
p.then(response => console.log(response));
p.catch(error => console.log(error));
或者使用async包
async.race([
function(callback) {
request('/resource-that-may-take-a-while',callback)//下载图片 改成真正的下载
},
function(callback) {
setTimeout(function() {
callback(null, 'two');
}, 3000);
}
],
// main callback
function(err, result) {
});