Home / Web Development Technologies / AngularJS / Promises in an AngularJS Earth

Promises in an AngularJS Earth

Promises in an AngularJS

Promises in an AngularJs.

A promise terminology is an interface that arrangements with objects that are returned or get filled in at a future point in time (essentially, asynchronous activities). At its center, a promise terminology is an object with a then() function.
To showcase the focal points, let us take a sample where we have to fetch the present profile of a client:

var MyProfile = null;
var testvariable = ‘TestData’;

fetchServerConfig(function(serverConfig) {
fetchUserProfiles(serverConfig.USER_PROFILES, username,
     function(profiles) {
MyProfile= profiles.MyProfile;
});
});

There are a couple of issues with this methodology:

1. The resultant code is a space bad dream, particularly in the event that you need to chain numerous calls.
2. Errors reported in the middle of callbacks and capacities(functions) tend to be lost,unless you handle them physically/manually at every stride.
3. You need to exemplify the logic of what you need to do with MyProfile in the deepest callback, either straightforwardly, or through a different function.

The Promises settle these problems. Before we go into the how, how about we investigate the same issue actualized with promises:

var MyProfile =
fetchServerConfig().then(function(serverConfig) {
return fetchUserProfiles(serverConfig.USER_PROFILES, username);
}).then(function(profiles) {
return profiles.currentProfile;
}, function(error) {
  // Settle issues in either fetchServerConfig or
// getclientProfiles here
});

Notice the focal points:
1. You can chain capacity calls, so you don’t get into a space bad dream.
2. You are guaranteed that the past functions call is done before the up coming function in the chain is called.
3. Every then() call takes two contentions (each of them are function). The first is the achievement callback and the second one is the error handler.
4. If there should arise an occurrence of blunders in the chain, the error will get spread through to rest of the error handlers. So any error in any of the callbacks can be taken care of at last.

Shouldn’t something be said about resolve and reject, you inquire? Indeed, deferred in AngularJS is a method for making promises . Calling resolve on it satisfies the promise(calls the achievement handler),while calling reject on it calls the error handler of the promise.

Related Topics :

What is AngularJs

Angular-JS Dependency Injection

Angular-JS Templates and Data Binding

Angular-JS Lists Tables and Other Repeated Elements

Promises in an AngularJs. A promise terminology is an interface that arrangements with objects that are returned or get filled in at a future point in time (essentially, asynchronous activities). At its center, a promise terminology is an object with a then() function. To showcase the focal points, let us take a sample where we have to fetch the present profile of a client: There are a couple of issues with this methodology: 1. The resultant code is a space bad dream, particularly in the event that you need to chain numerous calls. 2. Errors reported in the middle of…

0%

User Rating: 2.43 ( 2 votes)

Check Also

AngularJS and Laravel Blade

How to use AngularJS and Laravel Blade together

How to use AngularJS and Laravel Blade together In this article I will show you …

AngularJs Templates and Data Binding

AngularJs Templates and Data Binding

AngularJS Templates and Data Binding Templates in Angular applications are just HTML documents that we …

Leave a Reply

Your email address will not be published. Required fields are marked *