如何在运行时在 ngResource 查询方法中设置超时

how to set timeout in ngResource query method on runtime

本文关键字:设置 方法 超时 ngResource 运行时 查询      更新时间:2023-09-26

我有一个服务保存我所有的 API 端点连接器。这些终结点连接器是 ngResource 对象。

在其中一些中,我覆盖了默认方法,例如getquery例如添加responseTransformers。我希望能够设置get方法timeout属性以在发送另一个请求之前停止一个请求,但我想在我的服务中不这样做,我定义了所有$resource

this.collections = $resource(BackendConfig.apiUrl + 'collections/:uid/', {}, {
    query: {
        isArray: false,
        method: 'GET',
        timeout: myTimeoutPromise
    },
});

但是在运行时,就在我执行此操作之前

Resource.collections.query().then(function () {});

通过分析代码,我得出的结论是,可以覆盖参数(第二个参数),但不能覆盖操作(第三个)。

我找到了可能(尚未实现)的解决方案:创建新服务,保留所有超时(假设中断器),在我需要的地方注入我的资源服务,并在那里定义超时承诺。有没有更方便的解决方案?

我不知道

你的$resources做什么,但也许你可以把你的承诺链接到一个提供者中。

$http.get( SOME_URL_HERE ).
  then( function(response){
    /*Resolve the first promise then chain your other requests*/
    var promises = [ ];
    promises.push ( $http.get( OTHER_URL ) );
    promises.push ( $http.get( AGAIN_OTHER_URL ) );
    return $q.all( promises );
  })
  .then ( function( responses){
    /*This 'then' is of the chaining*/
    reponses[0].something /*response of the first promise*/
    reponses[1].something /*response of the second promise*/
  })
  .catch( function(error){
    console.log( error );
  });