gpt4 book ai didi

javascript - 代码不等待回调函数完成

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

我是来自 Java 的 Javascript 新手,想要编写一个功能类似于 Java 方法的函数。问题是调用代码不会等待被调用函数完成,而是继续发生错误。我希望你明白我在说什么。也许你可以帮助我

if (Titanium.Facebook.loggedIn) {
// User is not logged in with his ACS Account but with its Facebook Account
// now we can check if he has an ACS Account with the same eMail Address
Titanium.API.info('User seems to be logged in to Facebook');

gFacebookAccount = gFacebookController.getLoggedInFacebookAccount();
Titanium.API.info('Logged In Facebook Account is : '+ gFacebookAccount); }

调用的 getLoggedInFacebookAccount 看起来像这样。我试图通过超时循环来延迟该方法,但不起作用。

exports.getLoggedInFacebookAccount = function()
{
Titanium.API.info('Started getLoggedInFacebookAccount');
var lCallBackFinished = false;
var lActualUser = null;
Titanium.Facebook.requestWithGraphPath('me', {}, 'GET', handleGraphRequestCallback);

waitForQueryCallback();


function waitForQueryCallback() {
if (!lCallBackFinished) {
Titanium.API.info('Wait for Query Callback Loop');
setTimeout(waitForQueryCallback, 100);
//
}
else
{
return lActualUser;
}
}


function handleGraphRequestCallback(pGraphCallback) {

// damn callbacks - now here the user is logged in
// and the Graph Request is (sucessfully or not) finished

Titanium.API.info('Start handleGraphRequest-Callback');
if (pGraphCallback.success) {
Titanium.API.info('Graph Request Callback Sucessful');
lActualUser = pGraphCallback.result;

} else {
Titanium.API.info('Graph Request Callback Not Sucessful');

}

lCallBackFinished = true;

}

}

控制台输出是:

[INFO][TiAPI   (  301)] User seems to be logged in to Facebook
[INFO][TiAPI ( 301)] Started getLoggedInFacebookAccount
[INFO][TiAPI ( 301)] Wait for Query Callback Loop
[INFO][TiAPI ( 301)] Logged In Facebook Account is : undefined
[INFO][TiAPI ( 301)] Function called
[ERROR][TiJSError( 301)] (main) [214,2413] ----- Titanium Javascript Runtime Error -----
[ERROR][TiJSError( 301)] (main) [0,2413] - In undefined:1,1

[ERROR][TiJSError( 301)] (main) [0,2413] - Message: Uncaught SyntaxError: Unexpected token u
[ERROR][TiJSError( 301)] (main) [1,2414] - Source: undefined
[ERROR][V8Exception( 301)] Exception occurred at undefined:1: Uncaught SyntaxError: Unexpected token u
[INFO][TiAPI ( 301)] Wait for Query Callback Loop
[INFO][TiRootActivity( 301)] (main) [0,0] checkpoint, on root activity resume. activity = de.dosofun.knowitall.Knowitall2Activity@44ede8f8
[INFO][TiAPI ( 301)] Wait for Query Callback Loop
[INFO][ARMAssembler( 60)] generated scanline__00000077:03515104_00000000_00000000 [ 33 ipp] (47 ins) at [0x3666d0:0x36678c] in 4662744 ns
[INFO][TiAPI ( 301)] Wait for Query Callback Loop
[INFO][TiAPI ( 301)] Wait for Query Callback Loop
[INFO][ActivityManager( 60)] Displayed activity de.dosofun.knowitall/.Knowitall2Activity: 5509 ms (total 5509 ms)
[INFO][TiAPI ( 301)] Wait for Query Callback Loop
[INFO][TiAPI ( 301)] Wait for Query Callback Loop
[INFO][TiAPI ( 301)] Wait for Query Callback Loop
[INFO][TiAPI ( 301)] Wait for Query Callback Loop
[INFO][TiAPI ( 301)] Start handleGraphRequest-Callback
[INFO][TiAPI ( 301)] Graph Request Callback Sucessful

行的原因:[INFO][TiAPI ( 301)] 登录的 Facebook 帐户是:未定义您可以看到代码不会等到被调用的方法完成。

什么是必要的,以便我的代码按预期工作,如果我的代码非常糟糕,什么是更好的方法(这不会导致阅读困惑)

感谢您的帮助

最佳答案

wait_var = waitForQueryCallback();
YOU ARE NOT HANDLING THE RETURN FROM waitForQueryCallback() here...

function waitForQueryCallback() {
if (!lCallBackFinished) {
Titanium.API.info('Wait for Query Callback Loop');
setTimeout(waitForQueryCallback, 100);
//
RETURN SOMETHING HERE?
}
else
{
return lActualUser;
}
}

尝试处理 waitForQueryCallback() 中的 wait_var 并在回调未完成时返回一些内容。

关于javascript - 代码不等待回调函数完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11598836/

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