DailyJS

AsyncTask, DefineJS ES6 Features

2014-12-12 00:00:00 +0000 by Alex R. Young

AsyncTask

Web workers are great, but who wants to go to the trouble of creating extra files just to run some code in parallel? AsyncTask (GitHub: gorillatron/async-task, License: MIT, npm: async-task) by Jørn Andre Tangen is a more generic web worker constructor that lets you define background tasks purely with JavaScript:

var task = new AsyncTask({
  doInBackground: function(a, b) {
    return a + b
  }
});

task.execute(1, 2)
  .then(function(result) {
    result === 3
  })
  .catch(handleException);

It has a fluent API, and has Karma/Mocha unit tests as well.

DefineJS: ES6 Generator Syntax

Recently I wrote about Mehran Hatami's define.js project, which is a lightweight AMD implementation. It has just been updated to support ES6 generators for loading modules, which means you can do this:

var module = yield require('module');

It's still asynchronous, but it looks synchronous. I think this is a really cool use of generators, and apparently several people asked Mehran to implement this so it seems like there's demand for it.