Promise fulfill the request time-out processing (Basic)

First, if the request is not a member timeout:

 

var http = require('http');

var url = require('url');

 

function get(addr) {

  return new Promise(function(resolve, reject) {

    var url_obj = url.parse(addr);

    was options = {

      hostname: url_obj.hostname,

      path: url_obj.path,

      method: 'GET'

    };

 

    var req = http.request(options, function(res) {

      res.setEncoding ( 'utf8');

 

      , data = '';

      res.on('data', function (chunk) {

        data += chunk;

      });

      res.on('end', function () {

        data = JSON.parse(data);

        resolve(data);

      });

    });

 

    req.on('error', function(e) {

      reject (e)

    });

    req.end();

  });

}

 

get('http://demos.so/result/homework.promise.userInfo').then(function (args) {

  return Promise.all([get('http://demos.so/result/userid=' + args['_id']), get('http://demos.so/result/student=' + args['_id'])]);

}).then(function (args) {

  console.log(args);

}).catch(function(err){

  console.log(err);

});

Look again at the code to overtime:

 

var http = require('http');

var url = require('url');

 

function delayPromise(ms) {

    return new Promise(function (resolve) {

        setTimeout(resolve, ms);

    });

}

function timeoutPromise(promise, ms) {

    var timeout = delayPromise(ms).then(function () {

            throw new Error('Operation timed out after ' + ms + ' ms');

        });

    return Promise.race([promise, timeout]);

}

 

function get(addr) {

  return new Promise(function(resolve, reject) {

    var url_obj = url.parse(addr);

    was options = {

      hostname: url_obj.hostname,

      path: url_obj.path,

      method: 'GET'

    };

 

    var req = http.request(options, function(res) {

      res.setEncoding ( 'utf8');

 

      , data = '';

      res.on('data', function (chunk) {

        data += chunk;

      });

      res.on('end', function () {

        data = JSON.parse(data);

        resolve(data);

      });

    });

 

    req.on('error', function(e) {

      reject (e)

    });

    req.end();

  });

}

 

timeoutPromise(get('http://demos.so/result/homework.promise.userInfo'),1000).catch(function (err) {

  console.log(err);

}).then(function (args) {

  return Promise.all([timeoutPromise(get('http://demos.so/result/userid=' + args['_id']), 1000), timeoutPromise(get('http://demos.so/result/student=' + args['_id']), 1000)]);

}).then(function (args) {

  console.log(args);

}).catch(function (err) {

  console.log(err);

});

 

 

  Principle is very simple, is to use Promise.race, we first create a Promise, which is treated with setTimeout, then Promise Promise newly created and we used before "game" look.

Guess you like

Origin www.cnblogs.com/ygunoil/p/12106916.html