V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
Bonnnnnn
V2EX  ›  问与答

又没人知道阿里云函数计算为啥我的用 async 会 timeout。。。

  •  
  •   Bonnnnnn · 2020-08-27 09:06:38 +08:00 · 2012 次点击
    这是一个创建于 1331 天前的主题,其中的信息可能已经有所发展或是发生改变。
    上面已经建立了 db connection,之前也有非 async 的试过了,可以用。

    module.exports.handler = async (event, context) => {
    try{
    connection.connect();
    const result = await connection.query('SELECT * FROM user WHERE id=1');
    connection.end();

    callback(null, result);
    }
    catch(err){
    callback(err);
    }
    }
    10 条回复    2020-08-27 12:28:36 +08:00
    ccraohng
        1
    ccraohng  
       2020-08-27 09:45:00 +08:00 via iPhone
    连接操作是同步的吗?
    Administrat0r
        2
    Administrat0r  
       2020-08-27 09:47:02 +08:00   ❤️ 2
    如果你确定代码没问题的话,你这是无中生 callback ?
    Bonnnnnn
        3
    Bonnnnnn  
    OP
       2020-08-27 10:29:49 +08:00
    @Administrat0r 试了 return(result),还是 timeout
    mxT52CRuqR6o5
        4
    mxT52CRuqR6o5  
       2020-08-27 10:45:50 +08:00
    https://help.aliyun.com/document_detail/156876.html
    完全没看到文档有说支持 async
    yaodong0126
        5
    yaodong0126  
       2020-08-27 10:46:32 +08:00
    无中生有,可还行
    mxT52CRuqR6o5
        6
    mxT52CRuqR6o5  
       2020-08-27 10:47:17 +08:00
    非要用可以这么写
    exports.handler = function(event, context, callback) {
    (async function(){
    ...
    }).then(callback);
    };
    lonelymarried
        7
    lonelymarried  
       2020-08-27 10:55:57 +08:00
    它有个时间限制,超过这个时间就 timeout 了。
    mxT52CRuqR6o5
        8
    mxT52CRuqR6o5  
       2020-08-27 10:56:18 +08:00
    非要用可以这么写
    exports.handler = function(event, context, callback) {
    (async function(){
    ...
    return result;
    })().then(callback);
    };
    mxT52CRuqR6o5
        9
    mxT52CRuqR6o5  
       2020-08-27 11:03:34 +08:00
    仔细想了一下好像我前面说的都不对,当我没说
    你把非 async 没问题得代码发上来看看比一下有什么区别
    Bonnnnnn
        10
    Bonnnnnn  
    OP
       2020-08-27 12:28:36 +08:00
    破案了,当然我是忘了加 callback 。。。后面还有更致命的,我是从 http 函数,转到了事件函数,然后之前的一个 http 函数的触发一直挂在那,我忘了排查,删了就好了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5671 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 06:20 · PVG 14:20 · LAX 23:20 · JFK 02:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.