谁能解释一下 angularjs 中 $q 服务的使用? [英] Can anyone explain the use of $q service in angularjs?

查看:22
本文介绍了谁能解释一下 angularjs 中 $q 服务的使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 angularjs 的新手.我在 Restful api 调用中看到了 $q 来检查承诺.$q.defer() 用于保留承诺对象.我读到了承诺,但我什么也没得到.虽然我可以在没有 $q 的情况下进行 api 调用,但是它在文章的某处使用.

I am new to angularjs.I saw $q in restful api calls to check the promise. $q.defer() was used to retain the promise object. I read about the promises but I didn't get anything. although I can make the api call without $q, however it is used somewhere in articles.

所以我想知道 $q 的确切用法以及在没有 $q 的情况下进行 api 调用的区别.

So I want to know the exact use of $q and difference in making api calls without $q.

请帮忙.谢谢

推荐答案

我认为我写的关于 $q 的文章可能对您有所帮助.

I think the article I wrote about $q might help you.

$q 简介

$q 是一个角度定义的服务.它与 new Promise() 相同.但是 $q 通过增强附加功能让开发人员可以更轻松地执行复杂任务,从而将事情提升到一个新的水平.

$q is an angular defined service. It’s the same as new Promise(). But $q takes things to the next level by enhancing additional feature that developers can use to perform complex tasks more simply.

这是使用 $q 创建承诺的示例

This is a sample for creating a promise using $q

angular.module("app",[])
.controller("ctrl",function($scope,$q){
  var work = "resolve";
  var promise = $q(function(resolve, reject) {
    if (work === "resolve") {
        resolve('response 1!');
    } else {
        reject('Oops... something went wrong');
    }
  }); 
  promise.then(function(data) {
    alert(data)  

  }) 
})

$q.defer()

$q.defer() 返回 promise 构造函数的实例.创建 defer 对象后,您可以从该对象访问以下方法和属性

$q.defer() return the instance of the promise constructor. Once you create a defer object there are following methods and properties that you can access from that object

resolve(value) – 使用值解析派生的 promise.如果值是通过 $q.reject 构造的拒绝,则承诺将被拒绝.

resolve(value) – resolves the derived promise with the value. If the value is a rejection constructed via $q.reject, the promise will be rejected instead.

reject(reason) – 拒绝带有原因的派生承诺.这相当于使用通过 $q.reject 构造的拒绝来解决它.

reject(reason) – rejects the derived promise with the reason. This is equivalent to resolving it with a rejection constructed via $q.reject.

notify(value) - 提供 Promise 执行状态的更新.在承诺被解决或拒绝之前,这可能会被多次调用.

notify(value) - provides updates on the status of the promise's execution. This may be called multiple times before the promise is either resolved or rejected.

promise – {Promise} – 与此延迟关联的承诺对象

promise – {Promise} – promise object associated with this deferred

看例子

angular.module("app",[])
.controller("ctrl",function($scope,$q){
  var work = "resolve";

  function getData(){
    var obj = $q.defer();

    if (work === "resolve") {
        obj.resolve('response 1!');
    } else {
        obj.reject('Oops... something went wrong');
    }

    return obj.promise;
  } 
  getData().then(function(data) {
    alert(data)  

  }) 
})    

$q.all()

如果用户需要一次性发送多个请求,则可以使用 $q.all() 服务.

If a user need to send multiple request one shot,then the user can use $q.all() service.

 $q.all([$http.get('data1.json'),$http.get('data2.json')])
      .then(function(response){
        console.log(response[0].data) // data1.json response 
        console.log(response[1].data) // data1.json response 
 })

在这里,有两个 http 请求同时发送到两个单独的 JSON 文件以获取数据.响应以数组形式出现,响应顺序与 HTTP 请求顺序相同.

In here,there are two http request sent simultaneously to two separate JSON files to get data. The response comes as an array and response order is same as the HTTP request order.

$q.race()

$q.race() 与 $q.all() 非常相似.但是它不会发送每个请求的响应,而是只返回一个请求响应.具体来说,只返回已执行的第一个请求的响应.这并不意味着它不会发送其他请求.所有请求都在发送,但它只返回执行的第一个请求的响应.

$q.race() is very similar to $q.all(). But instead of sending response of each request, it will only return the one request response. Specifically, only return the response of first request that been executed. That does not mean it’s not going to send other requests. All the requests are sending but it's only return the response of the first request that executed.

 $q.race([$http.get('data1.json'),$http.get('data2.json')])
      .then(function(response){
        console.log(response[0].data) // return one response 
 })

这里的响应可以是 data1.Json 或 data2.json.这就是使用这种方法的缺点.由于它返回的是第一个执行的请求的响应,因此无法确定承诺会解决哪个请求响应.这种方法对于不想看到所有请求的响应的批量请求很有用

In here response can be either data1.Json or data2.json. That's the downfall of using this method. Since its return the response of the first executed request, can’t be sure which request response will resolved by the promise. This method useful for bulk requests which you don’t want to see the response of all the requests

结论

使用 $q 从非承诺对象/回调构造承诺,并利用 $q.all() 和 $q.race() 处理现有承诺.

Use $q for constructing promises from non-promise Objects/callbacks, and utilize $q.all() and $q.race() to work with existing promises.

这篇关于谁能解释一下 angularjs 中 $q 服务的使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆