gpt4 book ai didi

javascript - 将附加信息传递给 Promise 错误处理程序

转载 作者:行者123 更新时间:2023-12-02 16:03:45 25 4
gpt4 key购买 nike

我已经 promise 了 fbgraph nodeJS API 并使用它来测试功能。此功能将帖子发布到给定的 Facebook 群组,然后验证该帖子是否已正确通过并删除该帖子。此代码的目的是测试用户是否具有不同组数组的发帖权限。

当前使用 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 的信息,并预计它会崩溃。当我确实收到错误时,它会传递给我的处理程序并触发我的处理程序。这很好,但我希望能够引用 postsSuccessively bool 值来区分我期望收到的错误和意外的错误。

如何引用 postsSuccessively bool 值,或实现另一种优雅的方式来区分收到的错误?

最佳答案

也许我不太明白你想做什么,但生成器函数的好处之一是你可以在其中使用 try-catch-语句正如您所习惯的那样。

因此,您可以使用在文本中描述的方式进行操作

var facebookPoster = Promise.coroutine(function* (feedId) {
try {
var postId = (yield graph.postAsync(feedId + '/feed', sampleData)).id;
var returnedId = (yield graph.getAsync(postId)).id;
var postedSuccessfully = true;
var deleteResponse = yield graph.delAsync(postId);
var getAfterDeleteResponse = yield graph.getAsync(postId);
} catch (e) {
if (!postedSuccessfully) // ignore error, it's expected
throw e;
}
return postId;
});

facebookPoster(12345678910).then(console.log.bind(console), console.error.bind(console));

但是我认为以下内容更清晰:

var facebookPoster = Promise.coroutine(function* (feedId) {
var postId = (yield graph.postAsync(feedId + '/feed', sampleData)).id;
var returnedId = (yield graph.getAsync(postId)).id;
var deleteResponse = yield graph.delAsync(postId);
try { // sometimes deleteResponse is wrong, so double-check
var getAfterDeleteResponse = yield graph.getAsync(postId);
} catch (e) {
// ignore error, it's expected
} finally {
if (getAfterDeleteResponse)
throw new Error("hey it's still there while it shouldn't be!");
}
console.log(postId);
});

facebookPoster(12345678910).catch(console.error.bind(console));

或者更好的是,通过使用 then(success, fail) pattern 来避免可怕的 try-catch-finally 事情:


// sometimes deleteResponse is wrong, so double-check
var getAfterDeleteResponse = yield graph.getAsync(postId).then(function(post) {
throw new Error("hey it's still there while it shouldn't be!");
}, function ignoreExpectedError(){});

关于javascript - 将附加信息传递给 Promise 错误处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30969451/

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