任何人都可以解释在angularjs中使用$ q服务吗? [英] Can anyone explain the use of $q service in angularjs?

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

问题描述

我是angularjs的新手.我在宁静的api调用中看到$q来检查承诺. $q.defer()用于保留promise对象. 我读了诺言,但一无所获. 尽管我可以在不使用$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是一个角度定义的服务.与新的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构造函数的实例.创建延迟对象后,您可以从该对象访问以下方法和属性

$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构造的拒绝,则该Promise将被拒绝.

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)-提供有关诺言的执行状态的更新.在兑现承诺之前,可能会多次调用它.

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} – 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天全站免登陆