Skip to content

Instantly share code, notes, and snippets.

@zaaack
Created October 31, 2016 05:47
Show Gist options
  • Save zaaack/0ecbe976e03219c24a4a7303653d14bd to your computer and use it in GitHub Desktop.
Save zaaack/0ecbe976e03219c24a4a7303653d14bd to your computer and use it in GitHub Desktop.
koa-sync-middleware.js
let syncPromise
export default function syncMiddleware () {
return async (ctx, next) => {
if (ctx.isSync) { // 可以通过 decorator 实现
if (!syncPromise) {
syncPromise = next()
await syncPromise
} else {
syncPromise = syncPromise.then(next)
await syncPromise // 通过 promise.then 方法如果执行完了则立即执行,如果没有执行完才执行下一个控制器方法
}
} else {
await next()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment