The $q in promise and angular, defer

Source: Internet
Author: User

In the ES6 syntax, a new promise constructor is created that can be used to generate promise instances.

Promise object: Represents a future event that will occur (usually an asynchronous operation). With the Promise object, asynchronous operations can be expressed in a synchronous process, avoiding nested callback functions (commonly called ' callback hell ').

In Angularjs, an instance of an object deferred can also expose a derived promise instance. Here's a brief description:

1. $q:

$q is a built-in service for angular that enables you to execute functions asynchronously and allows you to use the return value (or exception) of a function when the function is executed.

2. Defer:

Defer literally means delay, $q. Defer () can create a deferred instance (a deferred object instance).

Deferred instances are designed to expose derived promise instances, as well as signal APIs that are used as successful or unsuccessful completion, as well as the status of the current task.

var deferred = $q. Defer ();  // registers a lingering object with the $q service deferred var promise = Deferred.promise;  // by deferred the deferred object, you can get a commit promise, and promise will return the completion result of the current task

Methods of Defer:

1. Deferred.resolve (value): Successfully resolved (resolve) its derived promise. The parameter value will be used in the future as Promise.then (Successcallback (value) {...}, errorcallback (reason) {...}, Notifycallback (notify) {...}) The parameters of the Successcallback function.

2. Deferred.reject (reason): The derived promise was not successfully resolved. The parameter reason is used to describe the cause of the unsuccessful. At this point, the Promise object of the deferred instance captures an error that the task did not execute successfully, Promise.catch (errorcallback (reason) {...}). To add, Promise.catch (errorcallback) is actually shorthand for promise.then (null, errorcallback).

3. Notify (value): Provides updates on the status of the promise ' s execution.

Here is a small example:

functionAsyncgreet (name) {vardeferred = $q. Defer ();//A deferred deferred object is created by $q.defer (), and when a deferred instance is created, a derived promise object is created, and deferred.promise is used to retrieve the derived promise. Deferred.notify (' About to greet ' + name + '. ');//the Notify method for lingering objects.   if(Oktogreet (name)) {Deferred.resolve (' Hello, ' + name + '! ');//The task was successfully executed}Else{deferred.reject (' Greeting ' + name + ' is not allowed. ');//The task was not successfully executed  }  returnDeferred.promise;//returns the Promise object for the deferred instance}functionOktogreet (name) {//just mock data, the actual code will be implemented according to the relevant business  if(name = = ' Superman ')return true; Else return false;}varPromise = Asyncgreet (' Superman ');//Get Promise Object//the then function of the Promise object obtains the current task, which is the execution state of the current deferred deferred instance. Its three callback functions are executed at resolve (), reject (), and notify () respectively.Promise.then (function(greeting) {alert (' Success: ' +greeting);},function(reason) {alert (' Failed: ' +reason);},function(update) {alert (' Got notification: ' +update);});

3. Promise:

When a deferred instance is created, the promise instance is also created. The deferred-derived promise can be retrieved by deferred.promise.

The purpose of promise is to allow interested parties to access the results of deferred task completion.

In accordance with the COMMONJS Convention, Promise is an interface that interacts with an object, indicating that the result of an action is asynchronous and that it is possible or impossible to complete at any given point in time. (This is a good line, my understanding is promise equivalent to a commitment, promise you this task at a given point in time may be completed, may not be completed.) If it is done it is equivalent to resolve, if not completed it is equivalent to reject. )

Methods of Promise:

1. Then (Successcallback, Errorcallback, Nitifycallback) is resolve/reject according to promise, or will be resolve/reject, Call Successcallback/errorcallback.

2. The abbreviation for catch (errorcallback) then (null, Errorcallback).

3. Finally (callback, Notifycallback)

Additional notes:

Promise.then () returns a new derivative promise that forms the promise chain.

Promiseb = Promisea.then (function(result) {  return result + 1;}); // Promiseb'll be resolved immediately after Promisea are resolved and its value // Would be is the result of Promisea incremented by 1

Original address: http://www.cnblogs.com/big-snow/p/5126059.html

The $q in promise and angular, defer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.