gpt4 book ai didi

javascript promise onSuccess 处理程序

转载 作者:行者123 更新时间:2023-12-02 16:49:18 24 4
gpt4 key购买 nike

是否有一个处理程序方法,与失败相对应,类似于成功,可以真正退出该异步队列并继续正常的函数调用。

让我详细说明一下。让我们说

getConnection()
.then(function(connection){
return self.getRecords() //some async routine, returns promise, does reject/resolve
})
.then(function(data){
return self.getDetail() //some async routine, returns promise, does reject/resolve
})
.then(function(data){ //I'm done here calling onResult, but this onResult may call several
self.onResult(data); //other functions down the road resulting in .fail() call
}) //I want to get out of this promise queue and continue with normal functions calls
.fail(function(info){
self.onFault(info); //any error onFault causes down the road shouldn't be q problem.
})
.done(function(){ //but this gets called all the time at end no matter success or fail
//release system resource like connection etc.
})

我试图在评论中解释这个问题,基本上我已经在 self.getDetail() 调用中完成了,一旦成功,我想退出 promise 队列,原因,如果 self.onResult( data) 之后出现了问题,.fail() 也会被触发,因为它有点依赖它。

我尝试将调用放入 .done() 方法中,但无论成功还是失败,done() 都会被调用。

我有一个失败的例程,由 .fail() 函数调用,但不知道是否有成功处理程序。

欢迎任何横向思考。

编辑 - 在 Barmar 发表评论后,我们可以这样做吗? (getConnection 返回一个 promise 并拒绝/解决

connections.getConnection(function(c){
return self.getMaster(c) //async routine, returns promise, does reject/resolve
}, function(info){
self.onFault(info) //any failures in getMaster, any error in onFault shouldn't be q business
})
.then(function(data){
return self.getDetail() //async routine, returns promise, does reject/resolve
}), function(info){
self.onFault(info)} //any failures in getDetail, any error in onFault shouldn't be q business
})
.fail(function(info){ //btw any errors, onFault causes down the road shouldn't be q problem- same for above onFault calls
self.onFault(info) //do I need this after above fail routines for each call?
})
.done(function(){ //ok, once everything's done, get out of promise queue
self.onResult(self.data) //any problem onResult causes down the road, should be it's own business
}) //release routine

//异步队列、 promise 链等之外的两个独立函数。这些函数中的任何错误不应影响 promise 链或调用它的失败处理程序。一连串的 promise 应该在那里完成。

onResult: function(data) {
console.log('do something with the data');
}

onFault: function(info) {
console.log('wonder what went wrong');
}

请建议进行上述编辑

我的主要主要要求,onResultonFault之后发生的任何事情都不应该是q库业务(fail),他们应该现在(以后)自己处理

最佳答案

传递给 then 的第一个函数本身就是一个成功处理程序。

类似操作的返回值:doSomething().then(function() { ... }) 是一个新的 Promise,您始终可以将其存储在变量中,然后使用多个独立的 then 调用:

var promise = someOperation().then(function(x) {
return doSomethingWith(x);
});
promise.then(function(processedX) {
// processedX is the return value of the function used to construct `promise`
// now for something completely different
}
promise.then(someOtherFunction);

您不需要无限期地链接,您始终可以通过将中间新的 Promise 存储到变量中并在其他地方使用它们(可能多次)来“脱离 Promise 链”,并创建多个独立的链。

这样,您就可以将一个 fail 处理程序附加到一个链中的同一个 Promise,而在另一个链中不附加任何处理程序。在您的情况下,您希望将整个链存储到变量中调用 self.onResult 的处理程序,对该变量使用 fail 处理程序,然后继续其余代码使用相同的变量。

关于javascript promise onSuccess 处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26820907/

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