Angular use promise to resolve multiple asynchronous callback problems

Source: Internet
Author: User

basic usage of promise

var p1 = new Promise (resolve, Reject) => {
  settimeout (Resolve, 1000, ' done ');
  }
  Promise Use function Pajax in P1.then (data=>{console.log (data),//Done
})
//angular
(dataurl,data { 
     var deferred = $q. Defer ();//Declaration deferred execution, indicating that you want to monitor subsequent execution 
     $http ({
         Url:dataurl, method
         : ' Get '  //POST  need to pass data
     }). Then (function (data, status, headers, config) { 
        deferred.resolve (data);//Declaration success, that is, HTTP request data successfully, can return data  
     }, function (data, status, headers, config) { 
        deferred.reject (data);//Declaration execution failed, that is, server returned error  
     }); 
    return deferred;
}

One obvious benefit of promise is that it can be used to solve the callback hell. Especially in situations where multiple callbacks are dependent on each other.
Using promise to resolve multiple asynchronous dependency calls

Promise provides a method Promise.all ([P1,P2]) for wrapping multiple promise instances into a new promise instance.

The received parameter is an array, and the P1 and P2 are all promise objects.
At this point the state of the promise.all depends on its parameters. In two different situations:

P1, p2 State is resolve, Promise.all state will become resolve;
As long as P1, p2 in a state of reject, then Promise.all state will become reject;
So we can use Promise.all () to solve multiple asynchronous dependency calls.
For example, we usually encounter a situation:
The site needs to obtain the user name first, and then according to the user name to get user information.

This gets the user name GetUserName () and gets the user information GetUser () is an asynchronous request to invoke the interface. Before obtaining the user information, you need to obtain the user name first.

That means getuser depends on the state of the getusername. So we can encapsulate these two requests through Promise.all () into a new promise object.

function Getuserpromise (promisex, Promisey) {return Promise.all ([Promisex, Promisey]). Then (values =>//returned Val 
   UEs an array of values that are returned by Promisex and Promisey. Console.log (values);  
  [V1, v2])} function GetUserName () {//let data = ' Superman '; Return to New Promise ((Resolve, Reject) => {//settimeout (Resolve (data), 1000);//})//} Let Dataurl = '
  /sss/sss '; Pajax (Dataurl)} function GetUser () {Let data = {id:1, username: ' Superman ', Gender: ' Male '}//return new Promise 
    ((Resolve, Reject) => {//settimeout (Resolve (data), 2000);
   Let Dataurl = '/sss/sss '; Pajax (Dataurl,data)} getuserpromise (GetUserName (), GetUser ()). Then (data => {//Here is the data that contains the GetUserName and 
An array of console.log (data) consisting of GetUser return values; [' Superman ', {id:1, username: ' Superman ', Gender: ' Male '}]} * * * Using the Promise chain Invoke * * Function GetUserName () {Let
  data = ' Superman '; return new Promise (Resolve, Reject) => {settimeout (Resolve (data), 4000); } function GetUser (username) {Let data = {id:1, username: ' Superman ', Gender: ' Male '} return new Pr
    Omise ((Resolve, reject) => {if (username) {settimeout (Resolve (data), 2000);
    } else{reject (' err '); }}) GetUserName (). Then (username => {return getuser ();}). Then (user => {console.log (user);}). catch (Err => {console.log (err);})

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.