Skip to content

Instantly share code, notes, and snippets.

@jsantell
Last active August 29, 2015 14:01
Show Gist options
  • Save jsantell/02025e2f29b8aee5d825 to your computer and use it in GitHub Desktop.
Save jsantell/02025e2f29b8aee5d825 to your computer and use it in GitHub Desktop.
Using Task.jsm in SDK
const { Task } = Cu.import("resource://gre/modules/Task.jsm", {});
const { defer, all } = require("sdk/core/promise");
const { setTimeout } = require("sdk/timers");
Task.spawn(function *() {
let item1 = yield getItem(1);
let [item2, item3] = yield all([getItem(2), getItem(3)]);
console.log(item1, item2, item3); // 1, 2, 3
}).then(function () {
console.log('all items processed')
});
function getItem (val) {
let { promise, resolve } = defer();
setTimeout(() => resolve(val);
return promise;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment