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

[FEATURE REQUEST] flush option #8

Open
jonathanong opened this issue Jan 30, 2016 · 2 comments
Open

[FEATURE REQUEST] flush option #8

jonathanong opened this issue Jan 30, 2016 · 2 comments

Comments

@jonathanong
Copy link

not sure how this would work API-wise, but the idea is to return a promise somewhere that resolves then there is no longer any promises in the queue.

const fn = throat(3, myOtherFunction)

fn(1)
fn(2)
fn(3)
fn(4)

fn.flush().then(doSomethingElse())

it should resolve immediately if there are no more functions in the queue. flush should throw if any of the promises in the queue failed.

@ForbesLindesay
Copy link
Owner

Interesting thought. This could be done as an extra wrapper:

// untested
function collector(fn) {
  var inProgress = 0;
  var waiting = [];
  function end() {
    inProgress--;
    if (inProgress === 0) {
      while (waiting.length) waiting.pop()();
    }
  }
  function run() {
    var result = fn.apply(this, arguments);
    inProgress++;
    result.then(end, end);
    return result;
  }
  function flush() {
    return new Promise(function (resolve) {
      if (inProgress) waiting.push(resolve);
      else resolve();
    });
  }
  run.flush = flush;
  return run;
}

Then:

const fn = collector(throat(3, myOtherFunction)

fn(1)
fn(2)
fn(3)
fn(4)


fn.flush().then(() => doSomethingElse())

Might be a useful utility function to publish?

@tejasmanohar
Copy link

@ForbesLindesay That would be an extremely useful utility function! 💯

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