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


Rare Computer History Memorabilia Being Auctioned By Bonhams
23/10/2024

Invitations handwritten and signed by Charles Babbage, seminal papers by  Alan Turing and Claude Shannon, a "Blue Box" phone hacking device, a prototype Apple Macintosh and an Apple Lisa 2/10 are [ ... ]



A Turing Machine In LEGO
06/10/2024

A Turing Machine is one of those abstract ideas that is much easier to undertand if you can see it working. Now there's a proposal on the LEGO Ideas site for a Working Turing Machine. Lend it your sup [ ... ]


More News

 

espbook

 

Comments




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

 

Last Updated ( Thursday, 01 October 2015 )