Git Product home page Git Product logo

Comments (4)

xxleyi avatar xxleyi commented on June 13, 2024 1

或许更好的解决方案:

  • 使用 async await 控制串行
  • 使用生成器将嵌套结构的 flow 展平为 effects 迭代器

from blogs.

omnip620 avatar omnip620 commented on June 13, 2024

贡献一种写法,用了一个辅助类来拉平数组。

function Helper(val) {
  this.val = val;
}

Helper.prototype.run = function () {
  const next = (i = 0) => {
    if (i === this.val.length) return;
    const fn = this.val[i]();
    fn instanceof Promise
      ? fn.then(next.bind(null, i + 1))
      : next(i + 1);
  }
  next();
}
const createFlow = (arr) => {
  const loop = (innerArr) => {
    let innerFns = [];
    innerArr.forEach((el) => {
      if (Array.isArray(el)) {
        innerFns = innerFns.concat(loop(el));
      } else if (el instanceof Helper) {
        innerFns = innerFns.concat(el.val);
      } else {
        innerFns.push(el);
      }
    })
    return innerFns;
  }

  return new Helper(loop(arr))
}

from blogs.

sl1673495 avatar sl1673495 commented on June 13, 2024

或许更好的解决方案:

  • 使用 async await 控制串行
  • 使用生成器将嵌套结构的 flow 展平为 effects 迭代器

是的 async 一定是更简洁的 也许是我想怀旧一下了 XD

from blogs.

271853754 avatar 271853754 commented on June 13, 2024
function Flow(effects = []) {
    this.tasks = effects.reduce((result, effect) => {
        if(effect instanceof Flow) {
            result.push(...effect.tasks);
        } else if(Array.isArray(effect)) {
            result.push(...effect);
        } else {
            result.push(effect);
        }
        return result;
    }, []);
}

Flow.prototype.run = function(callback) {
    return new Promise(async (resolve) => {
        for(let i = 0;i<this.tasks.length;i++) {
           const task = this.tasks[i];
           await task();
        }
        callback();
    });    
}


function createFlow(effects) {
    return new Flow(effects);
}

const log = console.log;

const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

const subFlow = createFlow([() => delay(1000).then(() => log("c"))]);

createFlow([
  () => log("a"),
  () => log("b"),
  subFlow,
  [() => delay(1000).then(() => log("d")), () => log("e")],
]).run(() => {
  console.log("done");
});

from blogs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.