-
-
Notifications
You must be signed in to change notification settings - Fork 235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
函数组合 compose redux-saga koa 洋葱模型 #65
Comments
function compose(...middlewares) {
return function (ctx) {
let index = -1;
function dispatch(i) {
index = i;
const fn = middlewares[i];
if (!fn) return Promise.resolve();
try {
return Promise.resolve(fn(ctx, () => dispatch(i + 1)));
} catch (err) {
return Promise.reject(err);
}
}
return dispatch(0);
};
}
async function middleware1(ctx, next) {
console.log("Middleware 1 before");
await next();
console.log("Middleware 1 after");
}
async function middleware2(ctx, next) {
console.log("Middleware 2 before");
await next();
console.log("Middleware 2 after");
}
async function middleware3(ctx, next) {
console.log("Middleware 3 before");
await next();
console.log("Middleware 3 after");
}
const fn = compose(middleware1, middleware2, middleware3);
fn({}).catch((err) => console.error(err)); |
有点疑惑,try catch无法捕捉Promise.resolve异步代码的的报错吧~ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The text was updated successfully, but these errors were encountered: