How to process multiple promise in AngularJS _ AngularJS

Source: Internet
Author: User
Promise is a method to process values asynchronously. promise is an object that represents the final possible return value or thrown exception of a function, when dealing with a remote object, we can regard it as a proxy of the remote object. This article introduces how AngularJS processes multiple promise and what is promise, for more information about how to use AngularJS to process promise, you may encounter multiple promise requests.

The simplest process is that every promise has then. As follows:

var app = angular.module("app",[]);app.controller("AppCtrl", function($q. $timeout){var one = $q.defer();var two = $q.defer();var three = $q.defer();$timeout(function(){one.resolve("one done");}, Math.random() * 1000)$timeout(function(){two.resolve("two done");}, Math.random() * 1000) $timeout(function(){three.resolve("three done");}, Math.random() * 1000) functioin success(data){console.log(data);}one.promise.then(success);two.promise.then(success);three.promise.then(success);})

Is there a better way?

The $ q. all method can accept an array of promise and is called as follows:

var all = $q.all([one.promise, two.promise, three.promise]);all.then(success);

What is promise?

Promise is a method to process values asynchronously. promise is an object that represents the final possible return value or thrown exception of a function, when dealing with a remote object, we can regard it as a proxy of the remote object. If promise is also a method of asynchronous processing, we will think of the difference between it and XHR and $. ajax?

Js normally uses closures or callbacks to return non-synchronous data, such as XHR requests after page loading. We can interact with the data normally, as if it has already returned, without relying on the triggering of the callback function.

So what problems does ng's promise aim to solve? The callback has been used for a long time. generally, if a callback depends on another callback, debugging becomes very difficult. after each call, the processing error must be displayed. The difference is that promise provides another abstraction: these functions return promise objects.

Why use promise

One of the gains from using promise is that it has escaped the fixed thinking logic of callback. Promise makes the asynchronous processing mechanism look more like synchronization. based on the synchronous function, we can capture the returned values and abnormal values as expected. We can capture errors at any time in the program and bypass subsequent code dependent on program exceptions. we don't need to think about the benefits of this synchronization. Therefore, the purpose of promise is to keep the code running asynchronously while obtaining the function combination and error bubbling capabilities.

Promise is a class object and comes with some conventions.

• Only One resolve or reject will be called.

• If promise is executed or rejected, the processing programs dependent on it will still be called.

• The handler is always called asynchronously.

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.