Koa: ES7 Async/Await question

Created on 16 Mar 2016  路  8Comments  路  Source: koajs/koa

Not sure if this is the right place to ask this question, and if it is not, then feel free to close the issue :)

Doing some research into ES7 async/await, I came across this article: https://spion.github.io/posts/es7-async-await-step-in-the-wrong-direction.html

Refering to the section, 'Loss of generality and power', I was wondering, is the argument in that article relevant to [email protected]? It does seem that generators, as used in [email protected] are much stronger than async functions, although the async/await pattern does seem to satisfy most use cases

question

Most helpful comment

i'm not sure what the article is saying (i only skimmed it). you could always do something like:

app.use(Bluebird.coroutine(function * (ctx, next) {
  yield next()
})

and replace Bluebird.coroutine() with whatever engine you want.

All 8 comments

To be fair; avoid using async/await and you've got your "generality" back. There's nothing forcing you to use async/await in koa@2.

const Koa = require('koa')
const app = new Koa()
app.use((ctx, next) => {
  ctx.state.myState = 'Hello '
  next()
})
app.use(ctx => {
  ctx.body = ctx.state.myState + ' World'
})

@fl0w you need return next() in your example.

@gyson yes, that's the intent. But this was a counter argument. The question was about if the syntax is to declarative; I was illustrating that this has nothing to do with koas v2 implementation. A middleware stack can be run as a function chain.

Obviously not recommended; but somewhat mutes OP's concern.

Hm... but what about this: https://github.com/koajs/koa/blob/v2.x/lib/application.js#L103
This means that if you cannot supply your own run engine, as per the article above anymore. What if I want to use bluebird Promise.coroutine for example? Or my own custom implementation?

@perrin4869 https://github.com/koajs/koa/pull/683/files

And what you're talking about is scope. You can't cover all needs and all use-cases now and in the future: "What if I wanted to use express middleware in koa@1, and not use generators/yield?". It's not koa's concern.

You can extend the Application class to override callback and use.

i'm not sure what the article is saying (i only skimmed it). you could always do something like:

app.use(Bluebird.coroutine(function * (ctx, next) {
  yield next()
})

and replace Bluebird.coroutine() with whatever engine you want.

closing for now!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wlingke picture wlingke  路  3Comments

rowild picture rowild  路  4Comments

ke1Del picture ke1Del  路  3Comments

tvq picture tvq  路  4Comments

usernameisalreadytaken2014 picture usernameisalreadytaken2014  路  4Comments