我已經promisified fbgraph nodeJS API並使用它來測試功能。此功能發佈到給定的Facebook羣組,然後驗證帖子是否已正確通過並刪除帖子。此代碼的目的是測試用戶是否擁有針對不同組的陣列的權限。將其他信息傳遞給Promises錯誤處理程序
目前使用Bluebird的Promise庫和生成器實現的代碼。
var examplePostId = 12345678910
facebookPoster = Promise.coroutine(function* (feedId) {
var postResponse = yield graph.postAsync(feedId + '/feed', sampleData);
var postId = postResponse.id
var getResponse = yield graph.getAsync(postId)
var returnedId = getResponse.id
var postedSuccessfully = true // <-- This is what I want to reference in my error handler.
var deleteResponse = yield graph.delAsync(postId)
var getAfterDeleteResponse = yield graph.getAsync(postId) // I expect this to throw an error since the post should already be deleted
return postId
})
facebookPoster(examplePostId).then(function(postId){
console.log(postId);
}).catch(function(err){
console.log(err); // How can I tell the handler that error is fine because postedSuccessfully is true?
})
下面是我的錯誤:FB的Graph API非常不可靠。即使帖子並未真正刪除,我仍會收到成功響應(此處記載爲:___)。
因此,在我的生成器中,我試圖第二次獲取postID的信息,並期待它爆炸。當我收到錯誤時,它會傳遞給我的處理程序,並觸發我的處理程序。這很好,但我希望能夠引用postedSuccessfully布爾值來區分我期望收到的錯誤和意外的錯誤。
如何引用postedSuccessfully布爾值,或者實現另一種優雅的方式來區分接收到的錯誤?
我真的不明白它。該布爾似乎總是「真實的」。你究竟想知道哪裏不對? – Bergi
[我如何訪問以前的承諾會導致.then()鏈]?(http://stackoverflow.com/q/28250680/1048572),但我會嘗試給出具體的答案 – Bergi
例如,如果第一個getAsync調用返回錯誤,它會將它傳遞給處理程序,並且在那個時候,如果有條件地檢查處理程序中的postingSuccessfully的值,處理程序可以知道在delAsync調用之前收到此錯誤被製造了。但是,如果處理程序可以檢查postingSuccessfully的值並確定它是真的,它就知道該帖子成功完成,並可以告訴我該權限用於發佈帖子。讓我知道這是否合理。 – Antoine