- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否有一个处理程序方法,与失败相对应,类似于成功,可以真正退出该异步队列并继续正常的函数调用。
让我详细说明一下。让我们说
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');
}
请建议进行上述编辑
我的主要主要要求,onResult
、onFault
之后发生的任何事情都不应该是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/
如果在主题上成功发布了学生,我想将StudentDto返回给控制器。但因为onSuccess是在响应返回给控制器之后调用的。如果StudentDto成功发布,我有什么方法可以将其返回到。。如果在主题上
我正在java中使用tapestry,并且在tml文件中有一个循环,每行都有一个按钮。单击按钮时,我需要将行中元素的值传递给 onSuccess() 方法。我已经在按钮中尝试过使用 t: contex
我正在尝试使用 AjaxOptions.OnSuccess 来调用 javascript 函数并将参数传递给它。我可以毫无问题地调用一个没有参数的基本函数,这只是参数传递。 这是我的 JS 函数:
是否有一个处理程序方法,与失败相对应,类似于成功,可以真正退出该异步队列并继续正常的函数调用。 让我详细说明一下。让我们说 getConnection() .then(function(connect
我是 java/android 开发新手,所以如果可以的话请提供帮助。 我想要一个从 Web 服务(异步)获取数据的方法,一旦完成(onSuccess 或 onFailure),它应该调用 Activ
我有以下表格: @using (Ajax.BeginForm("DoComment", "Publication", new { id = Model.Publication.OID, pare
这是我在控制台中得到的: onSuccess(int, Header[], JSONObject) was not overriden, but callback was received 我已验证,
我正在开发一个基于 json 的应用程序。我想将数据从一个类传递给在 onSuccess 方法中接收的另一个类。当我请求数据时,数据将被正确接收,但是当我将该数据调用到其他类时,将发生空指针错误。我的
我使用 jqGrid 进行内联行编辑,我想让用户指示行更新是否成功。 问题是 onSuccess 事件在行编辑更新后没有触发,实际上没有任何操作事件被触发。 注意:我仅使用 JSON 服务器数据,没有
我做了 google connect 让用户使用他们的 google 帐户登录。 我所做的是,创建一个按钮 然后创建onSignIn() function onSignIn(googleUse
我正在使用 BrightFutures当我运行以下代码时,sequence().onComplete 和 sequence().onSuccess 在 geoCoder completionHandl
我正在尝试使用 Future 将一些基本功能链接在一起s 从一个光滑的 Action 中返回,我遇到了一些非常微不足道的绊脚石。 两者andThen和 onSuccess方法需要一个 PartialF
在 ASP.NET MVC 3 应用程序中,我使用 Ajax.BeginForm 将写入的文本发布到 Controller 。 @using (Ajax.BeginForm("Post", "Foru
如果RPC方法返回Void,那么它会调用onSuccess()方法吗?或者我必须返回一些值才能使 RPC 调用 onSuccess() 方法? 最佳答案 如果异步请求成功完成,无论其返回值如何,onS
我在 PageMethod 调用中使用了 onSuccess 和 onFailure。然而,它们都没有被调用,WebMethod 也没有被解雇。 alert("1"); PageMethods.Log
有人可以告诉我下面的代码怎么了。主要问题是该行: fDbListAllClients; // this is not being "called" 似乎没有执行。 也可能有其他错误代码。我不担心
我有一个 IndexedDB,其中包含页面上各种元素的属性。我对其中一个属性有一个索引,并使用键范围来获取特定的结果列表。 var key = IDBKeyRange.bound(10, 20); v
因此,我在 onSuccess 中有一些辅助行为,例如分析等。而且我需要传递给跟踪,不仅是查询/变异的结果(在这种情况下是变异),而且还有我传递的 arg。似乎只有将它附加到返回“数据”我才能做到这一
首先,之前对 SO 类似问题的回答根本没有帮助我。 有时调用executeTransactionAsync()时我没有在 onSuccess 中的 UI 线程上获取刷新数据方法。 例如,在下面的代码
我正在尝试使用以下结构将数据保存到 firebase root --clothing ----clothingImgDownloadUrl ------title ------category 据我了
我是一名优秀的程序员,十分优秀!