SlideShare a Scribd company logo
Angular promises and http
Angular promises and http
function loadPage{ 
$('#spinner').show(); 
setupPage(); 
$('#spinner').hide(); 
} 
function loadPage{ 
$('#spinner').show(); 
setTimeOut(function(){ 
setupPage(); 
$('#spinner').hide(); 
}, 0); 
}
JavaScript is single threaded (*mostly); 
Long running process will kill user experience: 
UI elements will be unresponsive; 
animations will pause 
Long running task will timeout; 
Most I/O APIs are async: 
, , ; 
Timers: setTimeout(), ; 
Other APIs
The Event Loop is a queue of callback functions; 
When an async function executes, the callback 
function is pushed into the queue; 
The JavaScript engine doesn't start processing the 
event loop until all the code was executed; 
The event loop is a first-in-first-out (FIFO) queue;
Angular promises and http
Callbacks: 
function doSomethingAsync(callBack){ 
setTimeout(function(){ 
//..... 
callBack(); 
}, 200); 
} 
doSomethingAsync(function(){ 
//will be called after 200ms 
}); 
Callbacks Hell: 
function doSomethingAsync(callBack){ 
//... 
} 
doSomethingAsync(function(result){ 
//... 
doSomethingAsync(function(result){ 
//... 
doSomethingAsync(function(result){ 
//... 
}); 
}); 
});
function doSomethingAsync(callBack){ 
//... 
} 
var promise = doSomethingAsync(); 
promise 
.then(function(result){ 
//... 
if(!ok) throw new Error(); 
return doSomethingAsync2(); 
}) 
.then(function(result2){ 
//... 
}, function(reason){ 
// Handle the error 
});
Angular promises and http
A promise is always async; 
A promise represents a task that will finish in the 
future; 
A promise expose a function ( ) : 
returns a new promise; 
allows for the attachment of that will be 
executed based on state; 
handlers are guaranteed to execute in order 
attached; 
Promises become by a value; 
Promises get by exceptions;
Angular promises and http
Angular promises and http
Angular uses promises for everything that is async: 
; 
; 
; 
much more.
//Service 
function doSmth(){ 
defered = $q.defer(); 
Result of 
asyncOperation(function(error, data){ 
if (!error) defered.resolve(data); 
else defered.reject(error); 
}); 
return defered.promise; 
} 
//Controller 
doSmth().then(function(value){ 
console.log(value); 
}, function(err){ 
console.error(err); 
}); 
console.log("The value will be shown after this.");
Result of 
doSmth() 
.then(function(value){ 
return value + 10; 
}, 
errorFn, 
notifyFn) 
.then(function(value){ 
return $q.reject(new Error()); 
}) 
.catch(function(err){ 
console.err(err); 
}) 
.finally(callbackFn);
Angular promises and http
core Angular service for communication with remote 
HTTP servers; 
can communicate via the browser's 
or via ; 
works hand in hand with service.
$http({method: 'GET', url: '/someUrl'}) 
.success(function(data, status, headers, config) { 
// this callback will be called asynchronously 
// when the response is available 
}) 
.error(function(data, status, headers, config) { 
// called asynchronously if an error occurs 
// or server returns response with an error status. 
}); 
*Since the returned value is a , you can also use for 
registering callbacks.
$http.get 
$http.head 
$http.post 
$http.put 
$http.delete 
$http.jsonp 
$http.patch
Angular promises and http
Example Link
Example Link

More Related Content

PDF
Angular server-side communication
PDF
Angular Promises and Advanced Routing
PPTX
AngularJS - $http & $resource Services
PPTX
Avoiding callback hell in Node js using promises
PDF
Workshop 23: ReactJS, React & Redux testing
PPTX
Angular 1.x vs. Angular 2.x
PPTX
AngularJS $http Interceptors (Explanation and Examples)
PDF
JavaScript Promise
Angular server-side communication
Angular Promises and Advanced Routing
AngularJS - $http & $resource Services
Avoiding callback hell in Node js using promises
Workshop 23: ReactJS, React & Redux testing
Angular 1.x vs. Angular 2.x
AngularJS $http Interceptors (Explanation and Examples)
JavaScript Promise

What's hot (20)

PPTX
Template syntax in Angular 2.0
PPTX
AngularJS Services
PDF
Workshop 14: AngularJS Parte III
PPTX
AngularJS Routing
PPTX
Angular 2 Architecture
PDF
Advanced redux
PDF
Rntb20200805
 
PPTX
JavaScript Sprachraum
PDF
React native-firebase startup-mtup
 
PDF
Rails is not just Ruby
PDF
Excellent
DOCX
VPN Access Runbook
PDF
How Angular2 Can Improve Your AngularJS Apps Today!
PPTX
Aspdevice - Asp Fast Crud introdution
PDF
정오의 데이트 for iOS 코드 정리
PPTX
AngularJS Directives
PDF
Asynchronous programming patterns in Perl
PDF
Testowanie JavaScript
PPTX
Component lifecycle hooks in Angular 2.0
Template syntax in Angular 2.0
AngularJS Services
Workshop 14: AngularJS Parte III
AngularJS Routing
Angular 2 Architecture
Advanced redux
Rntb20200805
 
JavaScript Sprachraum
React native-firebase startup-mtup
 
Rails is not just Ruby
Excellent
VPN Access Runbook
How Angular2 Can Improve Your AngularJS Apps Today!
Aspdevice - Asp Fast Crud introdution
정오의 데이트 for iOS 코드 정리
AngularJS Directives
Asynchronous programming patterns in Perl
Testowanie JavaScript
Component lifecycle hooks in Angular 2.0
Ad

Viewers also liked (8)

PDF
AngularJS Best Practices
PPT
Coffee@DBG - Exploring Angular JS
PPTX
Basics of angular directive (Part - 1)
PDF
AngularJS Custom Directives
PPTX
Building an End-to-End AngularJS Application
PPTX
AngularJS custom directive
PPTX
Building AngularJS Custom Directives
PDF
Angular js routing options
AngularJS Best Practices
Coffee@DBG - Exploring Angular JS
Basics of angular directive (Part - 1)
AngularJS Custom Directives
Building an End-to-End AngularJS Application
AngularJS custom directive
Building AngularJS Custom Directives
Angular js routing options
Ad

Similar to Angular promises and http (20)

PPTX
The Promised Land (in Angular)
PDF
Asynchronous development in JavaScript
PDF
The evolution of java script asynchronous calls
PPTX
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
PDF
Testing web APIs
PPTX
Async discussion 9_29_15
PPT
You promise?
PDF
Intro to Asynchronous Javascript
PDF
Async js - Nemetschek Presentaion @ HackBulgaria
PDF
The Strange World of Javascript and all its little Asynchronous Beasts
PPTX
Java Script Promise
PDF
Understanding Asynchronous JavaScript
PPTX
asyncjavascript.pptxdgdsgdffgfdgfgfgfdgfdgf
PDF
The evolution of asynchronous JavaScript
PDF
Sane Async Patterns
PDF
The evolution of asynchronous javascript
PDF
Avoiding callback hell with promises
PDF
Asynchronous programming done right - Node.js
PDF
Asynchronous JavaScript Programming with Callbacks & Promises
PDF
Developing Async Sense
The Promised Land (in Angular)
Asynchronous development in JavaScript
The evolution of java script asynchronous calls
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Testing web APIs
Async discussion 9_29_15
You promise?
Intro to Asynchronous Javascript
Async js - Nemetschek Presentaion @ HackBulgaria
The Strange World of Javascript and all its little Asynchronous Beasts
Java Script Promise
Understanding Asynchronous JavaScript
asyncjavascript.pptxdgdsgdffgfdgfgfgfdgfdgf
The evolution of asynchronous JavaScript
Sane Async Patterns
The evolution of asynchronous javascript
Avoiding callback hell with promises
Asynchronous programming done right - Node.js
Asynchronous JavaScript Programming with Callbacks & Promises
Developing Async Sense

More from Alexe Bogdan (6)

PDF
Dependency Injection pattern in Angular
PDF
Client Side MVC & Angular
PDF
HTML & JavaScript Introduction
PDF
Angular custom directives
PDF
AngularJS - dependency injection
PDF
AngularJS - introduction & how it works?
Dependency Injection pattern in Angular
Client Side MVC & Angular
HTML & JavaScript Introduction
Angular custom directives
AngularJS - dependency injection
AngularJS - introduction & how it works?

Recently uploaded (20)

PPTX
Introduction about ICD -10 and ICD11 on 5.8.25.pptx
PDF
Triggering QUIC, presented by Geoff Huston at IETF 123
PPTX
CSharp_Syntax_Basics.pptxxxxxxxxxxxxxxxxxxxxxxxxxxxx
PPTX
introduction about ICD -10 & ICD-11 ppt.pptx
PDF
RPKI Status Update, presented by Makito Lay at IDNOG 10
PPTX
PPT_M4.3_WORKING WITH SLIDES APPLIED.pptx
PPTX
ENCOR_Chapter_10 - OSPFv3 Attribution.pptx
PPTX
Introduction to Information and Communication Technology
PDF
“Google Algorithm Updates in 2025 Guide”
PPTX
international classification of diseases ICD-10 review PPT.pptx
PPTX
INTERNET------BASICS-------UPDATED PPT PRESENTATION
PDF
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
PPTX
QR Codes Qr codecodecodecodecocodedecodecode
PDF
www-codemechsolutions-com-whatwedo-cloud-application-migration-services.pdf
PDF
LABUAN4D EXCLUSIVE SERVER STAR GAMING ASIA NO.1
PDF
Behind the Smile Unmasking Ken Childs and the Quiet Trail of Deceit Left in H...
PPTX
durere- in cancer tu ttresjjnklj gfrrjnrs mhugyfrd
PDF
Unit-1 introduction to cyber security discuss about how to secure a system
PDF
Tenda Login Guide: Access Your Router in 5 Easy Steps
PDF
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
Introduction about ICD -10 and ICD11 on 5.8.25.pptx
Triggering QUIC, presented by Geoff Huston at IETF 123
CSharp_Syntax_Basics.pptxxxxxxxxxxxxxxxxxxxxxxxxxxxx
introduction about ICD -10 & ICD-11 ppt.pptx
RPKI Status Update, presented by Makito Lay at IDNOG 10
PPT_M4.3_WORKING WITH SLIDES APPLIED.pptx
ENCOR_Chapter_10 - OSPFv3 Attribution.pptx
Introduction to Information and Communication Technology
“Google Algorithm Updates in 2025 Guide”
international classification of diseases ICD-10 review PPT.pptx
INTERNET------BASICS-------UPDATED PPT PRESENTATION
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
QR Codes Qr codecodecodecodecocodedecodecode
www-codemechsolutions-com-whatwedo-cloud-application-migration-services.pdf
LABUAN4D EXCLUSIVE SERVER STAR GAMING ASIA NO.1
Behind the Smile Unmasking Ken Childs and the Quiet Trail of Deceit Left in H...
durere- in cancer tu ttresjjnklj gfrrjnrs mhugyfrd
Unit-1 introduction to cyber security discuss about how to secure a system
Tenda Login Guide: Access Your Router in 5 Easy Steps
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...

Angular promises and http

  • 3. function loadPage{ $('#spinner').show(); setupPage(); $('#spinner').hide(); } function loadPage{ $('#spinner').show(); setTimeOut(function(){ setupPage(); $('#spinner').hide(); }, 0); }
  • 4. JavaScript is single threaded (*mostly); Long running process will kill user experience: UI elements will be unresponsive; animations will pause Long running task will timeout; Most I/O APIs are async: , , ; Timers: setTimeout(), ; Other APIs
  • 5. The Event Loop is a queue of callback functions; When an async function executes, the callback function is pushed into the queue; The JavaScript engine doesn't start processing the event loop until all the code was executed; The event loop is a first-in-first-out (FIFO) queue;
  • 7. Callbacks: function doSomethingAsync(callBack){ setTimeout(function(){ //..... callBack(); }, 200); } doSomethingAsync(function(){ //will be called after 200ms }); Callbacks Hell: function doSomethingAsync(callBack){ //... } doSomethingAsync(function(result){ //... doSomethingAsync(function(result){ //... doSomethingAsync(function(result){ //... }); }); });
  • 8. function doSomethingAsync(callBack){ //... } var promise = doSomethingAsync(); promise .then(function(result){ //... if(!ok) throw new Error(); return doSomethingAsync2(); }) .then(function(result2){ //... }, function(reason){ // Handle the error });
  • 10. A promise is always async; A promise represents a task that will finish in the future; A promise expose a function ( ) : returns a new promise; allows for the attachment of that will be executed based on state; handlers are guaranteed to execute in order attached; Promises become by a value; Promises get by exceptions;
  • 13. Angular uses promises for everything that is async: ; ; ; much more.
  • 14. //Service function doSmth(){ defered = $q.defer(); Result of asyncOperation(function(error, data){ if (!error) defered.resolve(data); else defered.reject(error); }); return defered.promise; } //Controller doSmth().then(function(value){ console.log(value); }, function(err){ console.error(err); }); console.log("The value will be shown after this.");
  • 15. Result of doSmth() .then(function(value){ return value + 10; }, errorFn, notifyFn) .then(function(value){ return $q.reject(new Error()); }) .catch(function(err){ console.err(err); }) .finally(callbackFn);
  • 17. core Angular service for communication with remote HTTP servers; can communicate via the browser's or via ; works hand in hand with service.
  • 18. $http({method: 'GET', url: '/someUrl'}) .success(function(data, status, headers, config) { // this callback will be called asynchronously // when the response is available }) .error(function(data, status, headers, config) { // called asynchronously if an error occurs // or server returns response with an error status. }); *Since the returned value is a , you can also use for registering callbacks.
  • 19. $http.get $http.head $http.post $http.put $http.delete $http.jsonp $http.patch