node.js - request's pipe method, how to monitor the time required to read a file?
怪我咯
怪我咯 2017-05-24 11:38:32
0
1
713

To read a picture from the Internet, I need to know the time required. For example, if it exceeds 3 seconds, I will give up reading the picture. How to do this?

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(1)
淡淡烟草味

You can use promises

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));

Or use async package

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) {
    
});
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!