Skip to content
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

Open
Sunny-117 opened this issue Nov 3, 2022 · 2 comments
Open

函数组合 compose redux-saga koa 洋葱模型 #65

Sunny-117 opened this issue Nov 3, 2022 · 2 comments

Comments

@Sunny-117
Copy link
Owner

// 用法如下:
function fn1(x) {
  return x + 1;
}
function fn2(x) {
  return x + 2;
}
function fn3(x) {
  return x + 3;
}
function fn4(x) {
  return x + 4;
}
const a = compose(fn1, fn2, fn3, fn4);
console.log(a(1)); // 1+4+3+2+1=11


function compose(...fn) {
  if (!fn.length) return (v) => v;
  if (fn.length === 1) return fn[0];
  return fn.reduce(
    (pre, cur) =>
    (...args) =>
    pre(cur(...args))
  );
}
@kangkang123269
Copy link

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));

@tyust512
Copy link

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants