2014-11-24 36 views
0

當我調用這個函數時,它立即返回一個結果,而不是延遲到第三個承諾之後。我能做什麼??需要在多次承諾後返回結果

getBlogs: function(blogId){ 
    var blogcomments = new Entities.BlogCommentCollection(); 
    var blogs = new Entities.BlogCollection(); 
    var defera = $.Deferred(); 
    var deferb = $.Deferred(); 
    var deferc = $.Deferred(); 
    var model; 
    //alert(model); 

    $.get("/lightning/presentation/blogs", function(val){ 
     defera.resolve(val); 
    }); 

    var promisea = defera.promise(); 

    $.when(promisea).done(function(val){ 
     var models = initializeBlogs(JSON.parse(val)); 
     blogs.reset(models); 
     model = blogs.at(blogId); 
     //alert(JSON.stringify(model)); 
     $.get("/lightning/presentation/blogs/?blogId=" + blogId, function(full){ 
     deferb.resolve(full);    
     }); 
    }); 

    var promiseb = deferb.promise(); 

    $.when(promiseb).done(function(full){ 
     model.set('full', full); 
     //alert(JSON.stringify(model)); 
     $.get("/lightning/presentation/blogs/?comments=" + blogId, function(res){ 
     deferc.resolve(res);    
     }); 
    }); 

    var promisec = deferc.promise(); 

    $.when(promisec).done(function(res){ 
     if(res.length === 0){ 
     blogcomments.reset(); 
     }else{ 
     //alert(res) 
     var models = initializeBlogComments(JSON.parse(res)); 
     blogcomments.reset(models); 
     model.set('comments', blogcomments) 
     //return model; 
     } 
     currentBlog = model; 
     alert(JSON.stringify(model)); 
     //return model; 
    }); 
    //alert(JSON.stringify(model)); 
    return model; 
    }, 
+0

可能重複功能? - 異步代碼引用](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – hon2a 2014-11-24 12:30:17

回答

0

提高後,你已經給出了答案,你可以做這種方式,避免造成推遲額外,只是使用了$.when()已經返回的承諾。 :

getBlog: function(blogId, model){ 
    var blogcomments = new Entities.BlogCommentCollection(); 
    var fullBlog; 

    return $.when(

     $.get("/lightning/presentation/blogs/?blogId=" + blogId, function(full){ 
     fullBlog = full;  
     }), 

     $.get("/lightning/presentation/blogs/?comments=" + blogId, function(res){ 
     var models = initializeBlogComments(JSON.parse(res)); 
     blogcomments.reset(models);   
     }) 

    ).then(function() { 
     model.set('full', fullBlog); 
     model.set('comments', blogcomments); 
     return model; 
    }); 
    }, 

或者,你可以另外使用返回值從$.when()避免單獨的AJAX回調是這樣的:

getBlog: function(blogId, model){ 
    var blogcomments = new Entities.BlogCommentCollection(); 

    return $.when(
     $.get("/lightning/presentation/blogs/?blogId=" + blogId),  
     $.get("/lightning/presentation/blogs/?comments=" + blogId) 
    ).then(function(r1, r2) { 
     model.set('full', r1[0]); 
     var models = initializeBlogComments(JSON.parse(r2[0])); 
     blogcomments.reset(models);   
     model.set('comments', blogcomments); 
     return model; 
    }); 
    }, 
的[爲什麼是我的變量不變後,我修改它的內部
1

承諾是異步的。它們不會導致當前函數延遲返回(這將是同步)。他們回來了,這是一個將在未來某個時候解決的承諾。

因此,要解決你的代碼的最基本的方法是有來無回的模型,但承諾

getBlogs: function(blogId) { 
    var deferAll = $.Deferred(); 

    // your last set will then respond to them all 
$.when(promisec).done(function(res){ 
    if(res.length === 0){ 
    blogcomments.reset(); 
    }else{ 
    //alert(res) 
    var models = initializeBlogComments(JSON.parse(res)); 
    blogcomments.reset(models); 
    model.set('comments', blogcomments) 
    //return model; 
    } 
    currentBlog = model; 

    // THIS is where it gets resolved 
    deferAll.resolve(model); 
}); 

    // do whatever you need to 
    return deferAll.promise(); 
} 

然後調用getBlogs作爲

getBlogs(25).then(function(model) { 
    // model is given here 
}); 

不過,也有以更好的方式做這個。首先,你可以鏈接承諾。

$.get("/lightning/presentation/blogs/?blogId=" + blogId).then(function(full){...}).then().then() // etc 

最後,如果你真的要按照這樣的順序做多個異步事情,我可以推薦caolan優秀的異步庫嗎?它使得這樣更容易。 https://github.com/caolan/async

+0

謝謝。我也找到了另一種選擇,查看我的答案。我發現它最小:) – 2014-11-24 13:56:14

1
getBlog: function(blogId, model){ 
    var blogcomments = new Entities.BlogCommentCollection(); 
    var defer = $.Deferred(); 
    var fullBlog; 

    $.when(

     $.get("/lightning/presentation/blogs/?blogId=" + blogId, function(full){ 
     fullBlog = full;  
     }), 

     $.get("/lightning/presentation/blogs/?comments=" + blogId, function(res){ 
     var models = initializeBlogComments(JSON.parse(res)); 
     blogcomments.reset(models);   
     }) 

    ).done(function() { 
     model.set('full', fullBlog); 
     model.set('comments', blogcomments); 
     defer.resolve(model); 
    }); 
    return defer.promise(); 
    }, 
+0

當然,這也鎖定了他們。 – deitch 2014-11-24 14:00:50