代码不等待回调函数完成 [英] Code doesn't wait for Callback Function Completion

查看:159
本文介绍了代码不等待回调函数完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是来自Java的新的Javascript,想写一个像Java方法一样的函数。
问题是调用代码不等待被调用的函数完成,只是继续发生的错误。我希望你明白我在说什么。也许你可以帮助我

I am new to Javascript coming from Java and want to write a Function who acts like a Java Method. The Problem is that the calling Code doesnt wait for the called function to finish and just continues what occurrs errors. i hope you understand what I'm talking about. Maybe you can help me

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类似如下。我尝试通过超时循环延迟方法不工作。

The called getLoggedInFacebookAccount looks like this. i tried to delay the method by a timeout loop what doesnt work.

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帐户是:未定义
您可以看到代码不会等到调用方法结束。

cause of line: [INFO][TiAPI ( 301)] Logged In Facebook Account is : undefined you can see that the code doesnt wait until the called method is finished.

必要的是什么,所以我的代码工作原理,如果我的代码是非常糟糕,什么是更好的方法(这不是混乱阅读)

What is necessary so my code works as wished and if my code is very bad, what is a better way (which is not to confusing to read)

感谢您的协助

推荐答案

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;
}
}

尝试并处理wait_var从waitForQueryCallback如果回调未完成。

Try and handle wait_var from waitForQueryCallback() and return something if callback isnt finished.

这篇关于代码不等待回调函数完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆