Created
October 31, 2016 05:47
-
-
Save zaaack/0ecbe976e03219c24a4a7303653d14bd to your computer and use it in GitHub Desktop.
koa-sync-middleware.js
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
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