gpt4 book ai didi

JavaScript : Never returns from await

转载 作者:行者123 更新时间:2023-11-28 04:25:11 25 4
gpt4 key购买 nike

我正在尝试使用async/await,但await没有返回。

这是带有await的异步函数。

async _checkEmailDuplication() {
try {
console.log('Check Email Duplication is called');
let res = await MyNetwork.checkEmail(this.state.email);
console.log('Check email return value: ' + res);
if (res === 0) {
console.log('Available Email');
return false;
}
else {
console.log('Email Duplication Check Error');
return true;
}
}
catch (e) {
console.error('Check Email Call Error', e.message);
return true;
}
console.log('What happened?');
return true;
}

正在调用checkEmail函数,这里是检查Email函数定义

export type EmailCheckResponse = { ret?: number };

class Network {
constructor() {
this._network = NativeModules.NetworkService;
}
checkEmail = (email: string): Promise<EmailCheckResponse> => {
console.log('Network CheckEmail is called');
return this._network.checkEmail(email);
}
}

这是react-native模块并尝试使用iOS中的Nativemodules。

这是 Objective-C Native 模块导出部分。

RCT_EXPORT_METHOD(checkEmail:(NSString *)email 
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
@autoreleasepool {
[GRPCCall useInsecureConnectionsForHost:kHostAddress];

AccountService *client = [[AccountService alloc] initWithHost:kHostAddress];

EmailCheckRequest *request = [EmailCheckRequest message];
request.email = email;
NSLog(@"Requested Email: %@\n", request.email);

[client checkEmailWithRequest:request handler:^(EmailCheckResponse *response, NSError *error) {
NSLog(@"Email Check result: %d\n", response.ret);
}];
}
}

问题是调用checkEmail后,卡住了。没有返回。

日志说

2017-07-16 16:01:29.355 [info][tid:com.facebook.React.JavaScript] 检查电子邮件重复被调用2017-07-16 16:01:29.356 [info][tid:com.facebook.React.JavaScript] 网络 CheckEmail 被调用2017-07-16 16:01:29.364 [50446:3725293] 请求的电子邮件:xxx@xxx.com2017-07-16 16:01:29.384 [50446:3725170] 电子邮件检查结果:0

看来 Objective-C 模块已成功调用,但没有返回。调用await checkEmail后,没有任何反应。

有人可以告诉我的代码有什么问题吗? =(

最佳答案

我可以从你的React native 模块实现中看到,你忘记调用resolvereject方法,这对于工作 promise 的工作方式至关重要。

[client checkEmailWithRequest:request handler:^(EmailCheckResponse *response, NSError *error) {
NSLog(@"Email Check result: %d\n", response.ret);
if(response){
resolve(response);
}else{
reject(@"no_events", @"There were no events", error);
}
}
];

希望这对您有帮助。

关于JavaScript : Never returns from await,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45126086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com