只需在主干javascript fetch()调用完成后运行代码

只需在主干javascript fetch()调用完成后运行代码,javascript,jquery,backbone.js,Javascript,Jquery,Backbone.js,在下面的代码中,我试图将true从test2()返回到if块中的test1()。 与在test2()中一样,变量flag在fetch()success块中设置为true, 现在的问题是,由于主干fetch()异步调用,test2()将false返回到test1()if块(由于这一点,在test1()中不能使用if),并且在ajax调用完成后返回test2()success将标志设置为true,但这没有用,因为它已经将false返回到test1() 所以,请告诉我,只有在fetchsuccess块

在下面的代码中,我试图将
true
test2()
返回到
if
块中的
test1()

与在
test2()
中一样,变量
flag
fetch()
success
块中设置为
true
, 现在的问题是,由于主干
fetch()
异步调用,
test2()
false
返回到
test1()
if块(由于这一点,在
test1()
中不能使用if),并且在ajax调用完成后返回
test2()
success
标志设置为
true
,但这没有用,因为它已经将
false
返回到
test1()

所以,请告诉我,只有在fetch
success
块中设置了
标志
值时,如何在
test2()中设置任何延迟/或运行if块
我试过使用
\uuu.delay()
delay()
,但不适合我。 请在这个问题上提供帮助,因为我已经花了很多天的时间,但无法解决这个问题

test1 :function()
{
    var x = true;
    if(x && this.test2())// call to test2
     {
         // run the code here
     }
},

test2: function()
{ 
    var flag = false;// default value
    noteCaseTeam = App.data.createBean('Module',{id:123});//Return the object of Team  
    noteCaseTeam.fetch(
    {
        success:function() // working fine
        {
            flag =true;//setting flag to true
            case_number_team  = noteCaseTeam.get('name');
        }
    }); // end of fetch function  
    if (flag) //Control is not reaching here due to "flag" default value
    {
        return true;
    }
    else {
        return false;
    }
},// end of test2

谢谢

不能使用异步调用返回
布尔值

您最好使用
sync
回调来运行
test1

this.listenTo(CaseTeam, 'sync', test1);
CaseTeam
应该在代码中可用


如果您提供简化的测试用例,我将改进我的答案。

您只需要了解JQuery承诺如何工作:

test1:function()
{
var x=真;
//调用test2()并等待它
this.test2().done(函数(响应){
if(x){
//在这里运行代码
}
});
},
test2:function()
{ 
var noteCaseTeam=App.data.createBean('Module',{id:123});//返回团队的对象
return noteCaseTeam.fetch().done(
功能(响应){
case_number_team=noteCaseTeam.get('name');
}
);
},
不要在您的问题标题中添加“紧急”之类的内容。这无关紧要,而且会妨碍人们看到你的问题。Seprately:请求帮助时,请花时间将代码格式化为可读的格式。您可以在自己的代码中使用您喜欢的任何支撑样式或缩进,但当要求其他人阅读并帮助您使用时,您应该使用一些模糊的常用样式。