我在结尾处放置了捕获,但它们至少在一个特定实例中返回空对象.对于任何不为人知的事情都是必要的,或者它只是搞砸了我?
$( document).ready(function(){
app.callAPI()//a chainable a RSVP wrapper around a jquery call, with its own success() fail() passing forward to the wrapper, so it will either be a resolved or rejected thenable to which is Now going to be chained
.then(
function(env) {
//set the property you needed Now
app.someSpecialEnvObj = env;
},
function(rejectMessage){
console.log('call.API() cant set some special env object..');
console.log(rejectMessage);
}
)
.catch(
function(rejectMessage){
if(rejectMessage){
//a just incase, DOES IT HAVE VALUE, for somebody that may have not done their homework in the parent calls?
console.log('you have some kind of legitimate error, maybe even in callAPI() that is not part of any problems inside them. you may have forgotton handle something at an early state, your so lucky this is here!)
} else {
console.log('can this, and or will this ever run. i.e., is there any value to it, when the necessity to already be logging is being handled in each and every then already, guaranteeing that we WONT be missing ANYTHING')
}
}
);
});
这是错的吗?或者它是否有某种用途,即使我仍然在所有父链接然后使用.then(解决,拒绝)方法的所有用法上使用错误/拒绝处理程序?
编辑:我希望更好的代码示例.我想我可能仍然在命名中使用某种反模式,我在我的例子中拒绝消息,这是jqXhr对象吗?
那么也许我应该正确地命名它们或者什么?即jqXhr?顺便说一句,我喜欢在每个then()内部当场拒绝它的原因,如果有错误,是因为这样我可以大量记录每个单独的电话,如果那里有特别的问题,这样我就不会不得不跟踪任何事情.微记录,因为我可以.
Promise正在以这种方式帮助开放调试世界.
这是我尝试过的三个例子.我更喜欢method1和method2,我决不会回到method3,这是我在promise地区开始的地方.
//method 1
app.rsvpAjax = function (){
var async,
promise = new window.RSVP.Promise(function(resolve, reject){
async = $.extend( true, {},app.ajax, {
success: function(returnData) {
resolve(returnData);
},
error: function(jqXhr, textStatus, errorThrown){
console.log('async error');
console.log({jqXhr: jqXhr, textStatus: textStatus, errorThrown: errorThrown});
reject({ jqXhr: jqXhr, textStatus: textStatus, errorThrown: errorThrown}); //source of promise catch data believe
}
});
$.ajax(async); //make the call using jquery's ajax, passing it our reconstructed object, each and every time
});
return promise;
};
app.callAPI = function () {
var promise =app.rsvpAjax();
if ( !app.ajax.url ) {
console.log("need ajax url");
promise.reject(); //throw (reject Now)
}
return promise;
};
//method 2
app.ajaxPromise = function(){
var promise, url = app.ajax.url;
var coreObj = { //our XMLHttpRequestwrapper object
ajax : function (method, url, args) { // Method that performs the ajax request
promise = window.RSVP.Promise( function (resolve, reject) { // Creating a promise
var client = new XMLHttpRequest(), // Instantiates the XMLHttpRequest
uri = url;
uri = url;
if (args && (method === 'POST' || method === 'PUT')) {
uri += '?';
var argcount = 0;
for (var key in args) {
if (args.hasOwnProperty(key)) {
if (argcount++) {
uri += '&';
}
uri += encodeURIComponent(key) + '=' + encodeURIComponent(args[key]);
}
}
}
client.open(method, uri);
client.send();
client.onload = function () {
if (this.status == 200) {
resolve(this.response); // Performs the function "resolve" when this.status is equal to 200
}
else {
reject(this.statusText); // Performs the function "reject" when this.status is different than 200
}
};
client.onerror = function () {
reject(this.statusText);
};
});
return promise; // Return the promise
}
};
// Adapter pattern
return {
'get' : function(args) {
return coreObj.ajax('GET', url, args);
},
'post' : function(args) {
return coreObj.ajax('POST', url, args);
},
'put' : function(args) {
return coreObj.ajax('PUT', url, args);
},
'delete' : function(args) {
return coreObj.ajax('DELETE', url, args);
}
};
};
app.callAPI = function () {
var async, callback;
async =app.ajaxPromise() ; //app.ajaxPromise() is what creates the RSVP PROMISE HERE<
if(app.ajax.type === 'GET'){async = async.get();}
else if(app.ajax.type === 'POST') {async = async.post();}
else if(app.ajax.type === 'PUT'){async = async.put();}
else if(app.ajax.type === 'DELETE'){ async = async.delete();}
callback = {
success: function (data) {
return JSON.parse(data);
},
error: function (reason) {
console.log('something went wrong here');
console.log(reason);
}
};
async = async.then(callback.success)
.catch(callback.error);
return async;
};
//method 3 using old school jquery deferreds
app.callAPI = function () {
//use $.Deferred instead of RSVP
async = $.ajax( app.ajax) //run the ajax call Now
.then(
function (asyncData) { //call has been completed, do something Now
return asyncData; //modify data if needed, then return, sweet success
},
function(rejectMessage) { //call Failed miserably, log this thing
console.log('Unexpected error inside the callApi. There was a fail in the $.Deferred ajax call');
return rejectMessage;
}
);
return async;
};
我也将其作为另一个备份运行在某个地方.
window.RSVP.on('error', function(error) {
window.console.assert(false, error);
var response;
if(error.jqXhr){
response = error.jqXhr;
} else {
//response = error;
response = 'is this working yet?';
}
console.log('rsvp_on_error_report')
console.log(response);
});
编辑错误示例
//one weird error I can't understand, an empty string("")?
{
"jqXhr": {
"responseText": {
"readyState": 0,
"responseText": "",
"status": 0,
"statusText": "error"
},
"statusText": "error",
"status": 0
},
"textStatus": "error",
"errorThrown": "\"\""
}
//another wierd one, but this one comes from a different stream, the RSVP.on('error') function
{
"readyState": 0,
"responseText": "",
"status": 0,
"statusText": "error"
}
解决方法:
I am putting catches at the end
这是他们的典型位置 – 您处理链中某处发生的所有错误.重要的是不要忘记完全处理错误,最后建议采用全能方式.
even if I use
onreject
handlers in all.then(…)
calls?
这有点奇怪.通常所有错误都在一个中心位置(最后的捕获)处理,但当然如果你想要你可以在任何地方处理它们然后继续链.
只需确保理解difference between an onreject
handler in a then
and in a catch
,您就可以自由使用它们.尽管如此,建议最终捕获当前回调本身的错误.
they are returning empty object in one particular instance atleast.
然后承诺搞砸了 – 它绝不应该没有理由拒绝.似乎是造成的
if ( !app.ajax.url ) {
console.log("need ajax url");
promise.reject();
}
if (!app.ajax.url)
return Promise.reject("need ajax url");
Is a catch necessary for anything unbekNownst?
并不是的.问题是捕获通常是一个包罗万象,甚至捕获意外的异常.所以,如果你可以区分它们,你会对意想不到的事情做些什么?
通常你会为那些设置某种全局unhandled rejection handler,这样你就不必确保在每个promise链的末尾手动处理它们.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。