Koa: Why was generators deprecated in favor of async/await?

Created on 26 Jul 2017  路  5Comments  路  Source: koajs/koa

I got the deprecation warning and was wondering why it was deprecated?

koa deprecated Support for generators will be removed in v3. See the documentation for examples of how to convert old middleware https://github.com/koajs/koa/blob/master/docs/migration.md

Was there a specific performance reason?
A memory allocation issue?
The idea that it would only ever return a single value?

I like async/await just fine. Just curious as to why fully deprecate it and not just let the user choose which style they liked best.

Most helpful comment

Because the actual implementations of generators is a pure hack to exactly the same thing of async await, async / await is slower, but the "right thing"

All 5 comments

Because the actual implementations of generators is a pure hack to exactly the same thing of async await, async / await is slower, but the "right thing"

Todays compliance with legacy mw signature is done through koa-convert, which wraps the generator. It's a "best effort" support and is not full-proof. I don't think it was ever ment to be a choice, but rather to ease upgradeability.

@EduardoRFS But was that not at a time when generators were a shim? Now that generators are baked into node.js can't it work with both?

@jsheely nop, generators is in node from node 0.12, a lot of time ago. Now async / await is shipped. You don't need, generator or async functions, you can purely return a promise from a function. With that you can use generators(using a coroutine lib), async functions or just a pure function

Thanks for the info

Was this page helpful?
0 / 5 - 0 ratings