await-keyword is taken from the newer versions of C# and VB.NET.
It's job is to pause the execution of a piece of code untill new data is availible,
and then continue off where it left.
This results in the ability to write async functions very easily without the need for callback-nesting.
For instance, let's asume we have a "magic" function called
load whos task it is to
perform a simple xhr-request and callback when the data is availible.
getUserusername passwordgetGroupsuseridcbuser: usergroups: groups;;;load'/getUser'username: usernamepassword: passwordifdatasuccesscbdatauser;elsecbnull;;load'/getGroups/' + userIdifdatasuccesscbdatagroups;elsecbnull;;
As you can see, there's a lot of nesting of functions, and it's not always
easy to understand what is going to happen and in what order. People who are
the user will be logged in immidiately, which isn't the case because our
(here ofcause I assume that the load-function has been written in such a way that
it is usable with the await-keyword and not just callbacks as in the previous
example, more on that later).
var user = await getUserusername password;var groups = await getGroupsuserid;returnuser: usergroups: groups;var data = await load'/getUser'username: usernamepassword: password;ifdatasuccessreturn datauser;elsereturn null;var data = await load'/getGroups' + userId;ifdatasuccessreturn datagroups;elsereturn null;
The second new feature is the
yield-keyword. The yield-keyword works in certain ways
almost like the
await keyword, with the exception that instead of waiting for
whatever you pass after the
yield-keyword it is returned to the calling function.
yield-keyword is simply put used to create generators (or iterators). A simple
example illustrates best how it works:
yield 1;yield 2;yield 3;var iter = testGenerator;console.logiternext; // outputs 1console.logiternext; // outputs 2console.logiternext; // outputs 3console.logiternext; // throws StopIteration
yield you can easily create functions that have no end, because only the data you
request is generated. For instance, one can create a simple fibionacci-generator like this:
var a = 1 b = 0;whiletrueyield a;b = a + b;yield b;a = a + b;
for..of-loop only works on iterators or objects that have the same signature.
for..of to loop though all the values generated from a generator. For instance,
if you take the function
runTest created in the previous section, you could
rewrite the code to this using
forvar n of testGeneratorconsole.logn;
continue works as one would expect, so to print all the fib-numbers less than 100
you could do this:
forvar n of fibifn > 100 break;console.logn;