Mastering JavaScript Promises
eBook - ePub

Mastering JavaScript Promises

Muzzamil Hussain

Share book
  1. 188 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Mastering JavaScript Promises

Muzzamil Hussain

Book details
Book preview
Table of contents
Citations

About This Book

This book is for all the software and web engineers wanting to apply the promises paradigm to their next project and get the best outcome from it. This book also acts as a reference for the engineers who are already using promises in their projects and want to improve their current knowledge to reach the next level. To get the most benefit from this book, you should know basic programming concepts, have a familiarity with JavaScript, and a good understanding of HTML.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Mastering JavaScript Promises an online PDF/ePUB?
Yes, you can access Mastering JavaScript Promises by Muzzamil Hussain in PDF and/or ePUB format, as well as other popular books in Computer Science & Application Development. We have over one million books available in our catalogue for you to explore.

Information

Year
2015
ISBN
9781783985500
Edition
1

Mastering JavaScript Promises


Table of Contents

Mastering JavaScript Promises
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Promises.js
The fall and rise of JavaScript
Google's contributions to JavaScript
Where Promises.js came in?
What is a promise?
Why do we need promise in JS?
Software prerequisites
Environment you need before getting started
Future, promise, and delay
Promise pipelining
Read-only views
States of a promise
How do we keep Promises.js in this book?
Browser compatibility
Summary
2. The JavaScript Asynchronous Model
Programming models
The single-threaded synchronous model
The multithreaded synchronous model
The asynchronous programming model
Densities with an asynchronous programming model
Why do we need to block the task?
Why not use some more threads?
Learning the JavaScript asynchronous model
How JavaScript implements an asynchronous model
Callbacks in JavaScript
Blocking functions
The mechanism of a callback function in JavaScript
Basic rules to implement callbacks
Handling callback hell
The events
The mechanism of event handling
DOM – event capture and event bubbling
A list of the most common events handlers
Triggering functions in response to events
Types of events in JavaScript
Interface events
Mouse events
Form events
W3C events
Microsoft events
Mozilla events
The publisher/subscriber
A brief account of the observer pattern
A formal definition of observer
The push and pull model
The advent of observer/push-pub
The drawbacks of observer/push-pub
The promises object
Summing up – the asynchronous programing model
Summary
3. The Promise Paradigm
Callback, revisited
Promise
Deferred
How do promise and deferred relate to each other?
Standard behaviors of the Promise API
Interactive promises
The states and return values of a promise
Common sequencing patterns
Stacked
Parallel
Sequential
Decoupling events and applications logic
Promises as event emitters
What promises prescribed not to do
Avoiding getting into callback hell
Avoiding the use of unnamed promises
Promises and exceptions
The fail method
The then method
Best practices to handle exceptions in promise
Make your exceptions meaningful
Monitor, anticipate, and handle exception
Keep it clean
Considerations while choosing a promise
Summary
4. Implementing Promises
How to implement promises
Implementations in Java
The util package of Java
The mechanics of Java to implement a promise
The core components of java.util.concurrent
Executor
Queues
Timing
Synchronizers
Concurrent collections
The implementation of promise by Java
CompletionService
ExecutorService
Future
Delay and DelayedQueue
FutureTask
Summing up Java and Promises.js
Say hello to JDeferred
A few words about Android Deferred Object
Use case 1 – object success and failure callbacks for a task
Use case 2 – merging several promises
Mechanics of JDeferred
Features of JDeferred
Playing with the code using JDeferred
Deferred object and promise
Deferred Manager
Runnable and callable
wait() and waitSafely()
Filters
Pipes
Ultimate JDeferred
Summary
5. Promises in WinRT
An introduction to WinRT
The evolution of WinRT
A little detail about WinJS
WinJS – its purpose and a distribution history
WinJS on GitHub
HTML5, CSS3, and JavaScript
WT with HTML5, CSS3, and JavaScript
The need for integrating promise with WT
Problems when using asynchronous programming
Jumpstarting promises
Writing a function that returns a promise
Adding a change handler for input elements
Error handling
Chaining promises using the then() and done() functions
Example 1A – downloading a web page to a file using two asynchronous functions
Example 1B – downloading a web page to a file using startAsync
Summary
6. Promises in Node.js
The V8 engine – the mechanics
The V8 engine in Google Chrome
The evolution of Node.js
A brief introduction to Node.js
Download and install Node.js
Node Package Manager – NPM
Choice of environment
Setting up the environment for Node.js
A simple node server
Things we learned so far
Node.js with the Q library
Moving ahead with Q
Propagation in Q
Chaining and nesting promises
Sequences in Q
Combination in Q
How to handle errors in Q in Node.js
Making progress with promises
Getting to the end of a chain of promises
Callback-based promises versus Q-based promises
A few words on delay, timeout, and notify
Q.delay()
Q.timeout()
deferred.notify()
Q.Promise() – another way to create promises
Static methods of Q
Promise as a proxy
Familiarizing Node.js – the Q way
Unbinds and its solution
Q support for tracing stacks
Making promise-based actions
Object handling promises
Decomposition of primitive access
View revisited
Aborting a promise
Q utilities for Node.js
Summary
7. Promises in Angular.js
The evolution of Angular.js
The structure of the Angular.js document
Getting started with Angular.js
Creating your first Angular.js file
Step 1 – create the HTML 5 doc
Step 2 – add the JavaScript file to it
How to use Angular.js on your local machine
What would be your preference for the server?
Key elements of Angular.js
Supplying scope data
Filtering data
Controlling scopes
Routing views
Implementing promises in Angular.js
The schematics of using promises in Angular.js
Promise as a handle for callback
Blindly passing arguments and nested promises
Deferred objects or composed promises
Dealing with the nested calls
Concurrency in Angular.js
The combination of success and error
The safe approach
Route your promise
Summary
8. Promises in jQuery
From where it started?
Behind the scenes – how does jQuery work?
Is your document ready to submit?
How to use jQuery
The syntax
Caching in jQuery
A sample example
Selectors
Event methods
JavaScript before and after jQuery
The solution – introducing promises in jQuery
Deferred in jQuery
$.Deferred().promise() in jQuery
Projecting a promise in jQuery
Joining promises with $.when
Your own $.Deferred process
The advent of promises in jQuery
Summary
9. JavaScript – The Future Is Now
ECMAScript 6 (ECMA 262)
harmony:generators
The Fibonacci series
The MEAN stack
Real-time communication in JavaScript
Internet of Thing...

Table of contents