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

请问为什么 async 函数中的 await 不起作用?总是返回 undefined 。

  •  
  •   hgjian · 2019-06-18 20:53:15 +08:00 · 6628 次点击
    这是一个创建于 1767 天前的主题,其中的信息可能已经有所发展或是发生改变。
    代码如下:

    async function test_async ( ) {
    var test_array = [ "a" , "b" , "c" ] ;
    ( function iterator ( city_number ) {
    if ( url_array [ city_number ].pathname == "/4401" ) {
    // if ( test_array.length == 3 ) {
    return "DD" ;
    /*
    //to do something.
    return new Promise ( function ( resolve, reject ) {
    resolve ( "dd" ) ;
    } ) ;
    */
    }
    iterator ( city_number + 1 ) ; // 迭代调用 函数自身, 执行下一个循环 ;
    });
    }

    async function Get_data ( ) {
    var temp_Variable = await test_async ( ) ;
    console.log ( temp_Variable ) ;
    }

    Get_data ( ) ;

    在 win10 平台下的 node.js ,CMD 中执行 文件后,总是 返回 undefined。

    顺带问下,不知道 V2 怎么粘贴代码格式,帖子没有显示缩进,有人说下怎么做吗?
    第 1 条附言  ·  2019-06-18 21:59:37 +08:00
    抱歉,代码贴错了,我改了一下:

    async function test_async ( ) {
    var test_array = [ "a" , "b" , "c" ] ;
    ( function iterator ( i ) {
    // if ( test_array [ i ] == "c" ) {
    if ( test_array.length == 3 ) {
    return "i" ;
    }
    iterator ( i + 1 ) ; // 迭代调用 函数自身, 执行下一个循环 ;
    })(0);
    }

    async function Get_data ( ) {
    var temp_Variable = await test_async ( ) ;
    console.log ( temp_Variable ) ;
    }

    Get_data ( ) ;
    第 2 条附言  ·  2019-06-18 22:09:59 +08:00
    async function test_async ( ) {
    var test_array = [ "a" , "b" , "c" ] ;
    ( function iterator ( i ) {
    if ( test_array [ i ] == "c" ) {
    console.log ( i ) ;
    return i ;
    }
    iterator ( i + 1 ) ; // 迭代调用 函数自身, 执行下一个循环 ;
    })(0);
    }

    async function Get_data ( ) {
    var temp_Variable = await test_async ( ) ;
    console.log ( temp_Variable ) ;
    }

    Get_data ( ) ;
    18 条回复    2019-06-18 23:18:12 +08:00
    salamanderMH
        1
    salamanderMH  
       2019-06-18 21:00:58 +08:00
    排版能不能调整下
    kwrush
        2
    kwrush  
       2019-06-18 21:07:29 +08:00
    你 promise 被注释掉了啊,而且 iterator 放()里什么目的,立即执行函数?那你后面要加()啊
    hgjian
        3
    hgjian  
    OP
       2019-06-18 21:30:18 +08:00 via Android
    @salamanderMH 请问怎么调整啊?我在发帖提示那找了下没找到说明
    hgjian
        4
    hgjian  
    OP
       2019-06-18 21:34:56 +08:00 via Android
    @kwrush 我在 function test_async ( ) { 前面加了 async 啊,不是说加了 async 会隐式返回 promise ,所以我注释掉了显示的 promise,然后进行测试;

    另外,我是执行 Get_data(),然后 Get_data 调用 test_async
    akmissxt
        5
    akmissxt  
       2019-06-18 21:47:38 +08:00
    iterator 括起来是什么意思?
    kwrush
        6
    kwrush  
       2019-06-18 21:51:24 +08:00
    @hgjian 你的 iterater 不会被执行,async 隐式返回 promise,但是你没给返回值,就 undefined 了
    hgjian
        7
    hgjian  
    OP
       2019-06-18 21:55:06 +08:00
    @akmissxt 抱歉,代码贴错了,我改了一下:

    async function test_async ( ) {
    var test_array = [ "a" , "b" , "c" ] ;
    ( function iterator ( i ) {
    // if ( test_array [ i ] == "c" ) {
    if ( test_array.length == 3 ) {
    return "i" ;
    }
    iterator ( i + 1 ) ; // 迭代调用 函数自身, 执行下一个循环 ;
    })(0);
    }

    async function Get_data ( ) {
    var temp_Variable = await test_async ( ) ;
    console.log ( temp_Variable ) ;
    }

    Get_data ( ) ;

    iterator 括起来是什么意思?===> 就是循环迭代 test_async 函数进行计算。
    参考得如下网址: https://blog.csdn.net/birdflyto206/article/details/72627912?tdsourcetag=s_pctim_aiomsg
    循环迭代应该没有问题吧?
    hgjian
        8
    hgjian  
    OP
       2019-06-18 21:58:57 +08:00
    @kwrush 抱歉,代码贴错了,我改了一下:
    async function test_async ( ) {
    var test_array = [ "a" , "b" , "c" ] ;
    ( function iterator ( i ) {
    // if ( test_array [ i ] == "c" ) {
    if ( test_array.length == 3 ) {
    return "i" ;
    }
    iterator ( i + 1 ) ; // 迭代调用 函数自身, 执行下一个循环 ;
    })(0);
    }

    async function Get_data ( ) {
    var temp_Variable = await test_async ( ) ;
    console.log ( temp_Variable ) ;
    }

    Get_data ( ) ;


    可以帮忙修改一下我参考一下吗?
    我参考得 blog.csdn.net/birdflyto206/article/details/72627912?tdsourcetag=s_pctim_aiomsg ,在我原本得项目里面,迭代是油执行的。
    hgjian
        9
    hgjian  
    OP
       2019-06-18 22:10:42 +08:00
    async function test_async ( ) {
    var test_array = [ "a" , "b" , "c" ] ;
    ( function iterator ( i ) {
    if ( test_array [ i ] == "c" ) {
    console.log ( i ) ;
    return i ;
    }
    iterator ( i + 1 ) ; // 迭代调用 函数自身, 执行下一个循环 ;
    })(0);
    }

    async function Get_data ( ) {
    var temp_Variable = await test_async ( ) ;
    console.log ( temp_Variable ) ;
    }

    Get_data ( ) ;


    应该是改成这样吧?
    rabbbit
        10
    rabbbit  
       2019-06-18 22:12:29 +08:00   ❤️ 1
    async function test_async() {
    ....var test_array = ["a", "b", "c"];
    ....return (function iterator(i) {
    ........// if ( test_array [ i ] == "c" ) {
    ........if (test_array.length == 3) {
    .............return "i";
    ........}
    ........iterator(i + 1); // 迭代调用 函数自身, 执行下一个循环 ;
    ....})(0);
    }

    async function Get_data() {
    .....var temp_Variable = await test_async();
    .....console.log(temp_Variable);
    }

    Get_data();
    kwrush
        11
    kwrush  
       2019-06-18 22:22:21 +08:00
    @hgjian 你的 test_async 没有返回值,相当于 Promise.resolve().then(temp_Variable => console.log(temp_Variable)); temp_Variable 是 undefined。你的 iterator 是立即执行函数,有自己的作用域,参考 rabbbit 写法
    hgjian
        12
    hgjian  
    OP
       2019-06-18 22:22:36 +08:00
    @rabbbit 感谢,我原本的代码应该是下面的,原来的贴错了

    ....async function test_async ( ) {
    ........var test_array = [ "a" , "b" , "c" ] ;
    ........( function iterator ( i ) {
    ................if ( test_array [ i ] == "c" ) {
    ........................console.log ( i ) ;
    ........................return i ;
    ................}
    ................iterator ( i + 1 ) ; // 迭代调用 函数自身, 执行下一个循环 ;
    ........} ) ( 0 );
    ....}

    ....async function Get_data ( ) {
    ........var temp_Variable = await test_async ( ) ;
    ........console.log ( temp_Variable ) ;
    ....}

    ....Get_data ( ) ;

    执行以后 ........console.log ( temp_Variable ) ; 显示的是 undefined
    hgjian
        13
    hgjian  
    OP
       2019-06-18 22:24:48 +08:00
    @kwrush 谢谢,我研究一下啊
    hgjian
        14
    hgjian  
    OP
       2019-06-18 22:30:37 +08:00
    @rabbbit 谢谢你的代码,我自己这条一下午也没有搞明白
    hgjian
        15
    hgjian  
    OP
       2019-06-18 22:32:17 +08:00
    @rabbbit 请问你的代码里面 function iterator(i) 是不是相当于是 闭包了?
    autoxbc
        16
    autoxbc  
       2019-06-18 22:50:30 +08:00
    代码有两处错误

    iterator(i + 1)
    这一句当进行递归时,要把下一次递归的返回值作为本次函数的返回值,这样递归结束才能层层返回
    改成这样
    return iterator(i + 1)

    第二处 #10 已经指明了,async 函数的返回值如果不是 promise,会被隐式包装为 promise。但是注意,你的 asnyc test_async() 并没有显式返回,那么被包装的其实是 undefined,再次 await 后得到的只能是这个 undefined

    另外 (function(){})() 结构一般叫做立即执行函数,把这个叫闭包是讹传

    贴代码用 markdown 语法,在发帖时有效,回帖不能用 markdown
    hgjian
        17
    hgjian  
    OP
       2019-06-18 22:59:41 +08:00
    @autoxbc 谢谢你的指导
    hgjian
        18
    hgjian  
    OP
       2019-06-18 23:18:12 +08:00
    感谢楼上各位的指导,问题解决,分享两个解决方案

    第一个是
    @autoxbc 的办法,采用 return iterator(i + 1) 的方式。

    async function test_async ( ) {
    var test_array = [ "a" , "b" , "c" ] ;
    return ( function iterator ( i ) {

    if ( test_array [ i ] == "c" ) {
    console.log ( "test_async ( ) 函数内部 " + i ) ;
    return i ;
    }
    return iterator ( i + 1 ) ; // 迭代调用 函数自身, 执行下一个循环 ;

    } ) ( 0 ) ;
    }

    async function Get_data ( ) {
    console.log ( test_async ( ) ) ;
    var temp_Variable = await test_async ( ) ;
    console.log ( temp_Variable ) ;
    }

    Get_data ( ) ;


    第二个是在 segmentfault 请教来的方案,借助一个中间变量实现:

    async function test_async ( ) {
    var test_array = ["a", "b", "c"] ;
    var a = null ; // 中间变量
    ( function iterator ( i ) {
    if ( test_array [ i ] == "c" ) {
    a = i ;
    } else {
    iterator ( i + 1 ) ; // 迭代调用 函数自身, 执行下一个循环 ;
    }
    } ) ( 0 ) ;
    console.log ( "test_async() 函数内部 :" + a ) ;
    return a ;
    }
    async function Get_data() {
    console.log ( test_async ( ) ) ;
    var temp_Variable = await test_async ( ) ;
    console.log ( temp_Variable ) ;
    }
    Get_data();
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2897 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 02:48 · PVG 10:48 · LAX 19:48 · JFK 22:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.