Using the AsyncAwait pattern, you can write any asynchronous function with a callback as last argument in a synchronous way.
var multiply = function (x, y, callback) {
callback(x * y);
}
Now, we want to do 1 * 2 * 3 * 4
using an asynchronous multiply function:
function main() {
multiply(1, 2, function (res) {
multiply(res, 3, function (res) {
multiply(res, 4, function (res) {
console.log(res);
})
})
})
}
main() // 24
We just wrote spaghetti code. Now let's look the same code with async
, await
and yield
:
var main = async(function () {
var [res] = yield await(multiply)(1, 2);
[res] = yield await(multiply)(res, 3);
[res] = yield await(multiply)(res, 4);
console.log(res);
});
main() // 24
Using async
and await
, we can easily combine asynchronous functions to write a new one:
var factorial = async(function (n, callback) {
var res = 1;
for (; n > 0; --n) {
[res] = yield await(multiply)(res, n);
}
callback(res);
});
We can either use it the old way:
var main = function () {
factorial(4, function (res) {
console.log(res);
})
}
main() // 24
or the new way:
var main = async(function () {
var [res] = yield await(factorial)(4);
console.log(res);
});
main() // 24
- Mozilla TaskJS: Generator, Promise
spawn(function*() {
var data = yield $.ajax(url);
$('#result').html(data);
var status = $('#status').html('Download complete.');
yield status.fadeIn().promise();
yield sleep(2000);
status.fadeOut();
});
- OkCupid TameJS: Code Rewrite
var res1, res2;
await {
doOneThing(defer(res1));
andAnother(defer(res2));
}
thenDoSomethingWith(res1, res2);
private static async Task<int> Sum2ValuesAsyncWithAssistance() {
Task<int> task1 = Task.Factory.StartNew(() => 1);
Task<int> task2 = Task.Factory.StartNew(() => 2);
int value1 = await task1;
int value2 = await task2;
return value1 + value2;
}
- Kaffeine: Code Rewrite, Doesn't work with loops.
get = {
err, data = jQuery.get! "/"
if !err, process data
}