What can we do to Achieve
It’ll be some easy code to get data from an outside API, for which I’ll use a fun little project involving quotes from Ron Swanson. Then it’ll print a message after 2 seconds since getting the data. Subsequently another message will be printed defining that the previous one was displayed.
All of the opertaion will share two functions:
You’ll notice that they both have two types of interface: promises or callbacks to allow communication using those two constracting methods. It’s similar to how most study that switched to using promise-based access handle backwards unity for callbacks.
IN THE MEAN TIME: CO
co is a very bright and simple library that uses alternator functions to achieve a similar reaction as async functions that were specified before.
For those not familiar with generators there are some simple examples on MDN that should tell you most of what you need to know: Generator.
First, let’s look at some code:
co wraps a generator function and naturally performs two things:
- it executes next() until there’s something left to do in the generator function
- it waits on yield for the async objects to resolve
co has a concept of ‘yieldables’ which are async objects that can be used with
yield and for which it’ll wait before constant code execution. Those are (according to https://github.com/tj/co#yieldables):
- thunks (functions)
- array (parallel execution)
- objects (parallel execution)
- generators (delegation)
- generator functions (delegation)
Two are mainly worth mentioning here:
arrays. They give an extremely easy way to attain the parallel execution of async code, similar to what Promise. All allows you to do. Let’s say we wish to get the quote and print the message as rapid as possible and we don’t care which arrive first:
The most troublesome part of using callbacks is the trust of descending into what is commonly called a ‘callback hell’. I’m sure most of you are beautiful familiar with the concept, but for those who aren’t just a easy example to illustrate:
In the last example you already initiated seeing callback nesting, and we notice what that leads to. So, to fix that we started using Promises. In most container they give you much cleaner code which is not only more informative but also a lot easier to read. The most useful thing is that they admit you to write your asynchronous code as ‘flat’ as possible, so you directly know what comes after each other, and in what array the code is being completed.
Right now most latest browsers (with the exception of IE and Opera Mini) and Node.js implement native assurance, and for those who don’t, you can still use one of many other implementations like Bluebird or Q.
But let’s get down to business, here’s the same example as before but using Promises:
TOMORROW: ASYNC FUNCTIONS
This way is based on a proposal that was originally submitted to be included in ES7 (2016). As you may know, ultimately only two features were accepted as a part of that discharg (exponential operator and
await are essentially two new keywords that own to write asynchronous code that looks like it’s synchronous. Let’s use an example:
If you selected to be on the bleeding edge of things, then you can use transpilers and already jump on the async/await train before native async functions are implemented. Be wary though, it’s still a stage 3 students, so it _may_ change. In any case, use what makes the most sense for you and your project, and consider the indication of using a particular solution. Next time I’ll try to write some more about the completion of all those solutions and how much overhead in your code you get from using them.