代码之家  ›  专栏  ›  技术社区  ›  tanmayghosh2507

typescript中的async await-不等待等待的函数完全执行

  •  0
  • tanmayghosh2507  · 技术社区  · 6 年前

    我在typescript文件中有以下场景:

    async function a() : Promise<void> {
      try {
        var result = await b(); 
        console.log('result=' + result);
      } catch(err){
    
      } finally {
        console.log('fin');
      }
    }
    
    async function b() : Promise<number> {
        let x: number = 1;
        console.log('start');
        // A long time taking http call - tried making the http request, both with or without await, but no change in result.
        // x got modified to 2 as a result of above call.
        console.log('x=' + x);
        return x;
    }
    a();
    

    所有这些都发生在一个单独的文件中,几乎超过了框架,我关心的是对于上面的代码,我希望输出 start x=2 result=2 fin . 但我得到的结果是 start result=undefined fin x=2 .

    我的tsconfig有以下功能:

    "target": "es2015",
    "module": "commonjs",
    "lib": ["es5","es2015"] 
    

    有人能帮我一下吗?谢谢

    0 回复  |  直到 6 年前