加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular $q 官方实例代码

发布时间:2020-12-17 10:05:49 所属栏目:安全 来源:网络整理
导读:// for the purpose of this example let's assume that variables `$q` and `okToGreet`// are available in the current lexical scope (they could have been injected or passed in).function asyncGreet(name) { var deferred = $q.defer(); setTimeout
// for the purpose of this example let's assume that variables `$q` and `okToGreet`
// are available in the current lexical scope (they could have been injected or passed in).

function asyncGreet(name) {
  var deferred = $q.defer();

  setTimeout(function() {
    deferred.notify('About to greet ' + name + '.');

    if (okToGreet(name)) {
      deferred.resolve('Hello,' + name + '!');
    } else {
      deferred.reject('Greeting ' + name + ' is not allowed.');
    }
  },1000);

  return deferred.promise;
}

var promise = asyncGreet('Robin Hood');
promise.then(function(greeting) {
  alert('Success: ' + greeting);
},function(reason) {
  alert('Failed: ' + reason);
},function(update) {
  alert('Got notification: ' + update);
});

The Deferred API

A new instance of deferred is constructed by calling$q.defer().

The purpose of the deferred object is to expose the associated Promise instance as well as APIs that can be used for signaling the successful or unsuccessful completion,as well as the status of the task.

Methods

  • resolve(value)– resolves the derived promise with thevalue. If the value is a rejection constructed viareject,the promise will be rejected instead.
  • rejectreason)– rejects the derived promise with thereason. This is equivalent to resolving it with a rejection constructed viareject.
  • notify)- provides updates on the status of the promise's execution. This may be called multiple times before the promise is either resolved or rejected.

Properties

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

The Promise API

A new promise instance is created when a deferred instance is created and can be retrieved by callingdeferredpromise.

The purpose of the promise object is to allow for interested parties to get access to the result of the deferred task when it completes.

  • thensuccessCallback,[errorCallback],51)">notifyCallback])– regardless of when the promise was or will be resolved or rejected,thencalls one of the success or error callbacks asynchronously as soon as the result is available. The callbacks are called with a single argument: the result or rejection reason. Additionally,the notify callback may be called zero or more times to provide a progress indication,before the promise is resolved or rejected.

    This methodreturns a new promisewhich is resolved or rejected via the return value of thesuccessCallback,51)">errorCallback(unless that value is a promise,in which case it is resolved with the value which is resolved in that promise usingpromise chaining). It also notifies via the return value of thenotifyCallbackmethod. The promise cannot be resolved or rejected from the notifyCallback method. The errorCallback and notifyCallback arguments are optional.

  • catch)– shorthand forpromise.(null)

  • finallycallback)– allows you to observe either the fulfillment or rejection of a promise,but to do so without modifying the final value. This is useful to release resources or do some clean-up that needs to be done whether the promise was rejected or resolved. See thefull specificationfor more information

  • 原文地址:https://docs.angularjs.org/api/ng/service/$q

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

      推荐文章
        热点阅读