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

2019-06-18 20:53:15 +08:00
 hgjian
代码如下:

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 怎么粘贴代码格式,帖子没有显示缩进,有人说下怎么做吗?
6636 次点击
所在节点    Node.js
18 条回复
salamanderMH
2019-06-18 21:00:58 +08:00
排版能不能调整下
kwrush
2019-06-18 21:07:29 +08:00
你 promise 被注释掉了啊,而且 iterator 放()里什么目的,立即执行函数?那你后面要加()啊
hgjian
2019-06-18 21:30:18 +08:00
@salamanderMH 请问怎么调整啊?我在发帖提示那找了下没找到说明
hgjian
2019-06-18 21:34:56 +08:00
@kwrush 我在 function test_async ( ) { 前面加了 async 啊,不是说加了 async 会隐式返回 promise ,所以我注释掉了显示的 promise,然后进行测试;

另外,我是执行 Get_data(),然后 Get_data 调用 test_async
akmissxt
2019-06-18 21:47:38 +08:00
iterator 括起来是什么意思?
kwrush
2019-06-18 21:51:24 +08:00
@hgjian 你的 iterater 不会被执行,async 隐式返回 promise,但是你没给返回值,就 undefined 了
hgjian
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
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
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
2019-06-18 22:12:29 +08:00
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
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
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
2019-06-18 22:24:48 +08:00
@kwrush 谢谢,我研究一下啊
hgjian
2019-06-18 22:30:37 +08:00
@rabbbit 谢谢你的代码,我自己这条一下午也没有搞明白
hgjian
2019-06-18 22:32:17 +08:00
@rabbbit 请问你的代码里面 function iterator(i) 是不是相当于是 闭包了?
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
2019-06-18 22:59:41 +08:00
@autoxbc 谢谢你的指导
hgjian
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();

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/575257

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX