简体   繁体   English

在承诺链的末尾捕捉错误

[英]Catching errors at the end of a chain of promises

I am using a factory to query data from Parse.com, this occurs many times in my ionic app and would like to apply a little more DRY to my code. 我正在使用工厂从Parse.com查询数据,这种情况在我的离子应用程序中多次发生,并且想对我的代码施加更多的DRY。

To call data I am using: 要调用我正在使用的数据:

ParseFactory.provider('Clients', query).getAll().success(function(data) {
    $localStorage.Clients = data.results;
}).error(function(response) {
    errorFactory.checkError(response);
});

And often run many of these back to back to get data from different classes on the loading of a page. 并经常背靠背运行其中的许多操作,以在页面加载时从不同的类获取数据。

Is it possible to use one error block at the end of all of these? 在所有这些结尾处是否可以使用一个错误块? like this: 像这样:

ParseFactory.provider('Favourites', query).getAll().success(function(data) {
    $localStorage.Favourites = data.results;
})
ParseFactory.provider('Somethings/', query).getAll().success(function(data) {
    $localStorage.Programmes = data.results;
})
ParseFactory.provider('UserItems', query).getAll().success(function(data) {
    $localStorage.UserExercises = data.results;
})
ParseFactory.provider('Customers', query).getAll().success(function(data) {
    $localStorage.Clients = data.results;
}).error(function(response) {
    errorFactory.checkError(response);
}); 

You could create helper method: 您可以创建辅助方法:

function query(resource, query) {
  function querySucceeded(data) {
    $localStorage[resource] = data.results;
  }

  function queryFailed() {}

  ParseFactory.provider(resource,  query)
     .getAll()
     .success(querySucceeded)
     .error(queryFailed);
}

and, then just call: 然后,只需调用:

query('Favourites', query);
query('Customers', query);

and so on. 等等。

Alternatively, you could factor queryFailed out, as such: 或者,您可以将queryFailed排除在外,如下所示:

function query(resource, query) {
  function querySucceeded(data) {
    $localStorage[resource] = data.results;
  }

  return ParseFactory.provider(resource,  query)
     .getAll()
     .success(querySucceeded);
}

function queryFailed() {


}

$q.all([
    query('Favourites', query1),
    query('UserItems', query2)])
    .error(queryFailed);

$q.all takes an array (or object) of promises, and returns a single one. $ q.all接受一个promise数组(或对象),并返回一个。

The returned promise is resolved when all the original promises are resolved. 当所有原始承诺均得到解决时,返回的承诺即得到解决。 It's rejected as soon as one of the original promises is rejected. 最初的承诺之一被拒绝后,它就会被拒绝。

So, what you can simply do is something like 因此,您可以简单地执行以下操作

var request = function(path) {
    return ParseFactory.provider(path, query).getAll();
};

var promises = {
    favourites: request('Favourites'),
    programmes: request('Somethings/'),
    exercises: request('UserItems'),
    customers: request('Customers')
};
$q.all(promises).then(function(results) {
    $localStorage.Favourites = results.favourites.data.results;
    // ...
}).catch(function() {
    // ...
});

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

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