loveyu
V2EX  ›  Node.js

为什么 node.js 的 http.get 之类的 API 默认不支持设置超时

  •  
  •   loveyu · Aug 28, 2015 · 5997 views
    This topic created in 3989 days ago, the information mentioned may be changed or developed.

    这是出于什么考虑,对比其他的语言,一般都会有一个默认超时时间。难道说是因为异步的这些东西就让程序员自己控制么?

    还是说有超时,但是我不会用?

    7 replies    2015-08-28 23:39:37 +08:00
    ixiaohei
        1
    ixiaohei  
       Aug 28, 2015
    回调机制模式的东西能设置超时么?求高手解答
    ixiaohei
        3
    ixiaohei  
       Aug 28, 2015
    @loveyu 再用回调设置超时,高明
    morethansean
        4
    morethansean  
       Aug 28, 2015
    因为这个 API 比较简陋。
    whimsySun
        6
    whimsySun  
       Aug 28, 2015   ❤️ 1
    所以可以用 request 和 superagent
    cheng007
        7
    cheng007  
       Aug 28, 2015
    你自己封装一个超时 get ?代码类似于下面,代码只供参考, JS 的 API 我记得不熟悉,思路大致如此
    function get (url, timeout, next ) {
    var is_timeout = false;
    setTimeout (timeout, function () {
    is_timeout = true;
    })
    request.get (url, function (err, data ) {
    if (is_timeout ) {
    return next (new Error ("timeout"));
    }
    next (err, data );
    });
    }
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5362 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 08:00 · PVG 16:00 · LAX 01:00 · JFK 04:00
    ♥ Do have faith in what you're doing.