Microsoft Rushes To Embrace The Future - Async In Edge
Written by Ian Elliot   
Thursday, 01 October 2015

Just as you struggle to get to grips with Promises in ECMAScript 2015, Microsoft has rushed ahead to provide async support in the Chakra engine and Edge. 

edgeicon

Asynchonous code is generally difficult in classic JavaScript, which is a strange situation given how much async code you generally have to write. Of course things got much more difficult with the increased use of JavaScript to create apps.

In ECMAScript 2015 Promises were introduced as a way of making async code look more like sync code. Most beginner and intermediate JavaScript programmers find Promises a bit mystifying. They seem simple when you first meet them, but they way that they actually work is subtle and if you really want to make full use of them you do need to understand the subtlety. 

For ECMAScript 2016 Promises are out and Async functions are in. Yes, I know you wait years for a JavaScript update to come along and then ....

The important part is that Async functions really make async easy to use and while Promises are used to implement Async functions you can more or less ignore them and how it all works. 

If you have an existing function which returns a Promise then you can use it in async function and this basically means ignoring the fact it is asynchronous. For example, if myFunc1 returns a Promise you can use it as an Async function:

async function myFunc2(){
 await myFunc1();
 rest of program;
}

That's it - nothing more, nothing complicated and almost no hint of asynchronous operation. If you mark a function as async you can use await within in it to call any function that returns a Promise. You don't need a callback and you don't need to define any of the Promise's methods. You can treat it as if your code pauses until the result of the awaited function completes and it returns its results. You can then write code that processes the results following the call just as if the function was synchronous. 

There are a lot more details concerning results and error handling but essentially if you just treat the code as synchronous it will all mostly work. 

Put simply async and await are the only sane way to do asynchronous programming. 

The only problem is that unless you are willing to use one of the JavaScript compilers like Babel to install a polyfil for you, there is no way of using async and await in JavaScript until ECMA2016 gets finished and implemented - or is there? 

Microsoft introduced async and await in C# and obviously thinks it's a good idea - so much so that the Chakra and Edge teams have jumped the gun and added it to Edge as an experimental feature that you can turn on. 

The EdgeDev Blog has lots of detail about how it is implemented, which is interesting, but the important point is that you don't need to know the detail, to make use of it. 

The following video also explains the basic ideas: 

 

 

Until async/await support is a little more widespread you can't really make much use of it, but you can use it to discover how easy it is.

So the future is here, but we can't make use of it much until it is more widely here. Given Edge is leaving IE behind it could be some time before this happens. 

edgeicon 

 

Banner


A Programming Career - Insights From JetBrains
07/02/2024

Why did  you choose to become a software developer? Did you switch from another career field? What aspects of your job are the most important? And do you code on weekends? See how your answers re [ ... ]



Turing Award Winners Give Talks
02/02/2024

A series of talks is being given by Turing Award winners and industry luminaries, and it's free online for anyone to watch. The series has been organized and given at Georgia Tech. The interests of th [ ... ]


More News

 

raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info

 

Last Updated ( Thursday, 01 October 2015 )