V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
loveyu
V2EX  ›  Node.js

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

  •  
  •   loveyu · 2015-08-28 08:43:22 +08:00 · 5353 次点击
    这是一个创建于 3169 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

    7 条回复    2015-08-28 23:39:37 +08:00
    ixiaohei
        1
    ixiaohei  
       2015-08-28 08:57:27 +08:00
    回调机制模式的东西能设置超时么?求高手解答
    ixiaohei
        3
    ixiaohei  
       2015-08-28 09:01:08 +08:00
    @loveyu 再用回调设置超时,高明
    morethansean
        4
    morethansean  
       2015-08-28 09:46:03 +08:00
    因为这个 API 比较简陋。
    kfll
        5
    kfll  
       2015-08-28 09:51:47 +08:00 via iPhone
    whimsySun
        6
    whimsySun  
       2015-08-28 11:25:03 +08:00   ❤️ 1
    所以可以用 request 和 superagent
    cheng007
        7
    cheng007  
       2015-08-28 23:39:37 +08:00
    你自己封装一个超时 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 );
    });
    }
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1054 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 23:46 · PVG 07:46 · LAX 16:46 · JFK 19:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.