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

reduce promise chain #37

Open
huixisheng opened this issue Dec 26, 2018 · 4 comments
Open

reduce promise chain #37

huixisheng opened this issue Dec 26, 2018 · 4 comments

Comments

@huixisheng
Copy link
Owner

https://github.com/Tencent/wepy/blob/fdfdbd390a81aab3b38758096d89f74405c3c10e/packages/cli/core/hook.js#L69-L103

hookAsyncSeq (key, ...args) {
    let rst = args;
    let hooks = this._hooks[key] || [];

    let count = 0;
    let allRst = [];
    let lastRst = rst;
    let argLength = args.length;

    if (hooks.length === 0) {
      return Promise.resolve(argLength === 1 ? args[0] : args);
    }

    return new Promise((resolve, reject) => {
      const iterateFunc = (pfn, cfn) => {
        return pfn.then(v => {
          if (!Array.isArray(v)) {
            v = [v];
          }
          if (count++ !== 0) {
            allRst = allRst.concat(v);
          }
          lastRst = v;
          return cfn.apply(this, lastRst);
        }).catch(e => {
          reject(e);
        })
      }

      hooks = hooks.concat(() => Promise.resolve());
      hooks.reduce(iterateFunc, Promise.resolve(args)).then(() => {
        resolve(argLength === 1 ? lastRst[0] : lastRst);
      });
    });
  }
@huixisheng
Copy link
Owner Author

@huixisheng
Copy link
Owner Author

huixisheng commented Dec 26, 2018

function func1() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve('func1');
    }, 100)
  });
}

function func2() {
  return 'func2';
}

function func3() {
  return 'func3';
}

// Promise.resolve().then(func1).then(func2).then(func3);
[func1, func2, func3].reduce((p, f) => {
    console.log(f);
    console.log(p);
    return p.then(f)
  }, Promise.resolve())
  .then(result3 => {
    console.log('reduce', result3);
  });


Promise.all([func1(), func2(), func3()])
  .then(([result1, result2, result3]) => {
    console.log('Promise.all', result1, result2, result3);
  });

const applyAsync = (acc, val) => acc.then(val);
const composeAsync = (...funcs) => x => funcs.reduce(applyAsync, Promise.resolve(x));
const transformData = composeAsync(func1, func2, func3);
transformData().then((result3) => {
  console.log('composeAsync', result3);
});


// [Function: func1]
// Promise { undefined }
// [Function: func2]
// Promise { <pending> }
// [Function: func3]
// Promise { <pending> }
// Promise.all func1 func2 func3
// reduce func3
// composeAsync func3


// let result;
// for (const f of [func1, func2, func3]) {
//   result = await f(result);
// }
// console.log(result)

参考

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

1 participant