简体   繁体   English

拒绝承诺中的处理

[英]reject handling in promises

The following code returns an RSVP promise from each getJSON method: 以下代码从每个getJSON方法返回一个RSVP承诺:

  getJSON('/login')
  .then(getJSON('/errors').then(function(users) {
    self.user = users;
  }))
  .then(getJSON('contacts').then(function(contacts) {
    self.contacts = contacts;
  }))
  .then(getJSON('companies').then(function(companies) {
    self.companies = companies;
    callback(self);
  }, function(err){
    console.log('does not get here')  
  }));

My understanding of promises is obviously wrong, I don't want to provide an error callback for each then and instead I thought that the error would be forwarded on to the next available error callback in one of the subsequent then functions. 我对Promise的理解显然是错误的,我不想为每个then提供错误回调,而是我认为该错误将被转发到随后的then函数之一中的下一个可用错误回调。

In the above code, the getJSON on line 2 will be rejected but it is not forwarded onto the error callback in the last then. 在上面的代码中,第2行的getJSON将被拒绝,但是在最后一个回调中不会转发到错误回调上。

Do I have to provide an error callback for each then. 然后,我是否必须为每个错误回调提供回调。 This does not seem any different from callback hell. 这似乎与回调地狱没有什么不同。

Is there any reason you're not making all requests at once? 您是否有理由不一次提出所有请求?

self.user = getJSON("login");
self.contacts = getJSON("contacts");
self.companies = getJSON("companies");
// not sure what "/errors" does or what you assign it to, add it if you please

//.hash will go through object properties and wait for all promises to resolve
return RSVP.hash(self).then(function(results){
    //here, self now contains .user .contacts and .companies
    callback(self); //DO NOT do this, instead return the promise
}).catch(function(err){
     //this is where all error handling goes
});

Note the return there, it's best to return the promise rather than call a callback, you can .then it from the outside. 注意return那里,这是最好的回报的承诺,而不是调用回调函数,你可以.then从外面。

I think you're chaining your promises in a wrong way. 我认为您在错误地履行诺言。 Your getJSONS are not executing in succession after the previous completes. 您的getJSONS在上一个完成之后没有连续执行。 When you call the first getJSON (getJSON('/login')), you are not passing two handlers to its then method. 调用第一个getJSON(getJSON('/ login'))时,没有将两个处理程序传递给then方法。 You are passing a new getJSON call. 您正在传递一个新的getJSON调用。 This means that, just after the call to getJSON finishes (but before the ajax call ends) you are executing the second getJSON (getJSON('/errors')). 这意味着,在对getJSON的调用完成之后(但在ajax调用结束之前),您正在执行第二个getJSON(getJSON('/ errors'))。 You are passing the resulting promise as the first argument of the then method of getJSON('/login'). 您将传递的结果作为getJSON('/ login')then方法的第一个参数传递。 By doing this, this promise will resolve or reject with the same value as getJSON('/errors'). 这样,此承诺将以与getJSON('/ errors')相同的值来解析或拒绝。 Okay but... 好吧...

.then(getJSON('companies').then(function(companies) {
    self.companies = companies;
    callback(self);
  }, function(err){
    console.log('does not get here')  
  }));

Here you are passing a new promise, the one returned by getJSON(companies) as the first argument of a then method. 在这里,您正在传递一个新的诺言,它是getJSON(companies)返回的诺言,作为then方法的第一个参数。 The promise to which this method belongs, when rejected, will try to call the function passed as the second argument ... but there's none! 该方法所属的Promise在被拒绝时将尝试调用作为第二个参数传递的函数...但是没有! So, because getJSON('companies') does not reject, you don't receive an error. 因此,因为getJSON('companies')不会拒绝,所以您不会收到错误。 I've rewritten your chain: 我已经重写了您的链:

getJSON('/login').then(
  function(users) {
      self.user = users;
      return getJSON('/errors');
  }).then(function() {
      return getJSON('contacts')
  }).then(function(contacts) {
      self.contacts = contacts;
      return getJSON('companies');
  }).then(function(companies) {
      self.companies = companies;
      callback(self);
  }, function(err){
    console.log('does not get here')  
  });

Now I think it should work fine. 现在,我认为它应该工作正常。 After a succesfull resolve of each promise, a function making a new getJSON request will execute, and it will return its promise. 在成功完成每个承诺的解析之后,将执行发出新的getJSON请求的函数,并且它将返回其承诺。 If any of them rejects, the rejection will pass through until a then with second argument is found, which happens at the end of the chain. 如果其中任何一个拒绝,拒绝将一直进行到找到带有第二个参数的then为止,这发生在链的末尾。 The last function(err) will capture anything that went wrong before that point. 最后一个函数(err)将捕获在此之前发生的所有错误。

Similar to Benjamin Gruenbaum's, but slightly more concise. 与本杰明·格林鲍姆(Benjamin Gruenbaum)相似,但更为简洁。

return RSVP.hash({
  user: getJSON("login");
  contacts: getJSON("contacts");
  companies: getJSON("companies");
}}).then(callback).catch(function(err){
     //this is where all error handling goes
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM