Starting from the basics of resolving and rejecting Promises, we will gradually move on to controlling flows by redirecting outcomes, making failures into successes, and vice versa, and modifying data to make it ready for display. Once you have familiarized yourself with Promises, we will review the many awesome ways of combining Promises, such as waiting for all tasks or grabbing the first task to complete. Finally, we will ensure that you fully master Promises with a section on extra Promises that will provide further useful information and examples. Throughout the course, you will apply every single feature you have discovered by creating meaningful widgets that utilize Promises, both in ES6 and in AngularJS.
This course will not only make you feel comfortable utilizing Promises when they are provided, but will also make you appreciate how much easier they can make your programming in the future. You will gain hands-on, real-life knowledge and experience of a great technology that is on the rise.
About The Author
Mathieu Chauvinc is a Managing Director at Red Ape Solutions and works on keeping the company at the bleeding edge of web technologies. On a daily basis, he leads the tech team of 15 and collaborates with them in building complex single page applications and neat hybrid mobile apps. He has been involved in web technologies for over a decade, from Django to Go, SQL to NoSQL, Mootools to Angular, continuously trying to keep the right balance between the excitement of new frameworks and the stability of proven ones. He always has a special place in his heart for Python and Django.
He holds a Master’s in Engineering from the German University Karlsruhe and a Master’s in Engineering from the French ENSAM (Paris). Despite having an engineering background, he has always had a passion for knowledge sharing, which follows him to this day, and he regularly conducts corporate or free training on various I.T. topics in his adoptive country of Malaysia. He is also the author of the Introducing AngularJS, Packt Publishing video course.
This video will offer an overview of the course.
Some services ($http) return promise objects when called, but other processes do not. Often, a developer will need to create their own asynchronous process, such as waiting for some user interactions or the completion of a navigator action. The $q service provides the tool to handle such cases.
Promises allow us to code parts of an asynchronous flow. Flows rarely follow a single direction; there are cases where things go awry. Promises prepare for these instances by having two possible outcomes: success and failure.
Asynchronous process flows that are handled by promises are all about clarity of code and outcomes. Here, we will learn about the various ways to set callbacks for each case:
Some processes and flows require repeated information to be passed at unexpected times. Other flows, though of an unknown duration, expect their outcome to be final. Promises do not allow for second chances and, thus, are great tools to cover the latter of these cases.
A lot of processes will need to conduct more than one task upon completion of an action. Although promises are strongly single-chance and single-value, when it comes to the callbacks, developers can add as many as they want, even after the promise has been settled, and, thus, allow for several actions to be added from various parts of an application.
Angular 2 is coming and has everyone guessing with its breaking changes and new features. We will review where promises stand in that major overhaul.
Promises that represent one action resolve with a certain value. This value may be suitable for one flow, but not for another. Returning a normal value allows you to modify the resolve value of a promise to make it more relevant to an outlying context. In practice, this is really useful when grabbing API content to be displayed to a user.
When abstracting processes in general and not only asynchronous ones, we make an assumption as to what successes and what failures are. However, depending on a situation, what appears to be a success for one block, could actually represent a failure for the whole flow. Promises make it very easy to switch from a success to a failure in the next promise, and we will learn how to accomplish this. We will also give practical examples as to why this would be a useful feature.
In the same vein as the previous video, this will cover cases where a certain block of a process needs to be transformed from a failure into a success. Promises allow us to do this by returning anything but a rejected promise.
More than one action is needed and wherever possible, it is better to complete these actions in parallel without having to wait for the other to complete. We will use two API calls and display data in a simple case where the calls are independent of each other.
Sometimes, several tasks need to be completed before a process can move on. A solution to this issue is to complete them one after the other. However, wherever possible, it is always more efficient to run tasks in parallel. $q.all allows you to combine promises in a way that must all complete before the surrounded process continues.
In some cases, we do not know how many times a particular asynchronous task is expected to be run before stopping it. Using promises, we will be able to handle "n" tasks in a series.
When a function needs to return a promise, for example, a service that's returning to a controller, but the return value is not guaranteed to be a promise, $q.when can be used to ensure that the return is indeed a promise.
As we will show in an example, not returning any value within a .catch can lead to unexpected behavior. We will see how to handle this correctly.
Given a list of tasks, we need to know as soon as a single one of them is complete Promise.all allowed us to wait for all. Promise.race will allow us to trigger as soon as any of the tasks are complete.
Packt has been committed to developer learning since 2004. A lot has changed in software since then - but Packt has remained responsive to these changes, continuing to look forward at the trends and tools defining the way we work and live. And how to put them to work.
With an extensive library of content - more than 4000 books and video courses -Packt's mission is to help developers stay relevant in a rapidly changing world. From new web frameworks and programming languages, to cutting edge data analytics, and DevOps, Packt takes software professionals in every field to what's important to them now.
From skills that will help you to develop and future proof your career to immediate solutions to every day tech challenges, Packt is a go-to resource to make you a better, smarter developer.
Packt Udemy courses continue this tradition, bringing you comprehensive yet concise video courses straight from the experts.