Switching to async/await from Task.jsm/yield

Kris Maglione kmaglione at mozilla.com
Thu Mar 16 22:49:11 UTC 2017

On Thu, Mar 16, 2017 at 06:39:57PM -0400, Mike Conley wrote:
>I'm all for it.
>For the people who are more familiar with async / await than I am, and
>have also used Task.jsm, are there any gotchas, or is it pretty much a
>drop-in replacement?

In general, I think there are fewer gotchas with async/await 
than with Task.jsm. The only one I can think of is that awaiting 
on the result of a star function won't do what you expect, and 
generally won't fail in an obvious way, so you need to be 
careful to also convert all star functions that you're using as 
task generators.


function* foo() {
  yield doAsyncThing();

  return 42;

Task.spawn(function* () {
  yield foo();

  yield thingThatDependsOnFoo();

needs to turn into:

async function foo() {
  await doAsyncThing();

  return 42;

(async () => {
  await foo();

  await thingThatDependsOnFoo();

>On 2017-03-16 6:29 PM, Dave Townsend wrote:
>> For a long time now we've been writing JS code that waits for promises
>> using Task.jsm and generator functions. Recently though the JS team
>> added support for the JS standard way of doing this, async/await:
>> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
>> Writing code in standard JS is always better for the web, makes it
>> easier to onboard new engineers and allows for better support in
>> developer tools. So I'd like to propose that we switch to the standard
>> way of writing these functions immediately. New code should use
>> async/await instead of Task.jsm going forwards.
>> Florian has some rough plans to automatically rewrite existing usages of
>> Task.jsm to the standard JS forms so for now don't worry much about
>> going and submitting patches to fix up existing code. Once that is done
>> we can remove Task.jsm from the tree.
>> Does anyone object to any of this?

Kris Maglione
Firefox Add-ons Engineer
Mozilla Corporation

Technology is dominated by two types of people: Those who understand
what they do not manage.  Those who manage what they do not
	--Putt's Law

More information about the firefox-dev mailing list