-
-
Save yeukhon/1f47a00fd1253e029356 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] = all([getItem(2), getItem(3)]); | |
console.log(item1, item2, item3); // 1, 2, 3 | |
}).then(function () { | |
console.log('all items processed') | |
}); | |
function getItem(val) { | |
let { | |
deferred, resolve | |
} = defer(); | |
setTimeout(() => resolve(val)); | |
return deferred; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The code above will print undefined but in the correct blocking order (we want the task block until all values returned).
The code below will print. Order is not right but at least we can get the values back.