You are previewing Mastering JavaScript Promises.
O'Reilly logo
Mastering JavaScript Promises

Book Description

Discover and explore the world of promises, one of JavaScript's most powerful concepts

In Detail

JavaScript is a tool for the gurus who create highly useful applications, but it has some limitations. To overcome these limitations, a concept called JavaScript promises is rising rapidly in popularity. Promises makes writing complex logics more manageable and easy.

This book starts with an introduction to JavaScript promises and how it has evolved over time. You will learn the JavaScript asynchronous model and how JavaScript handles asynchronous programming. Next, you will explore the promises paradigm and its advantages.

Finally, this book will show you how to implement promises in platforms used in project development including WinRT, jQuery, and Node.js.

What You Will Learn

  • Get a complete overview of the promises paradigm and its advantages

  • Discover the programming models that are used to build JavaScript

  • Execute the asynchronous programming model in JavaScript and understand its usefulness in modern web developmen

  • Employ promises in JavaScript to boost your project performance

  • Apply the best practices of promises in JavaScript to your platform of choice

  • Manage your JavaScript code in a more professional and maintainable manner

  • Utilize promises effectively and efficiently in different domains and disciplines of software engineering

  • Implement promises in WinRT, Node.js, jQuery, and Angular.js

  • Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.

    Table of Contents

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