done-criteria

Easy to use 'async.whilst' like utility

Usage no npm install needed!

<script type="module">
  import doneCriteria from 'https://cdn.skypack.dev/done-criteria';
</script>

README

done-criteria

Build Status

Easy to use async.whilst like utility.

install

$ npm install done-criteria

examples

unnamed

var DoneCriteria = require('../lib/done-criteria')
  , exit = false;

var doneCriteria = new DoneCriteria(['unko', 'geri'], function(error) {
  console.log('all done!');
  exit = true;
});

setTimeout(function() { doneCriteria.done('unko'); }, 1000);
setTimeout(function() { doneCriteria.done('geri'); }, 2000);

var loop = function() {
  if (exit) {
    process.exit(0);
  } else {
    console.log('not done yet...');
    setTimeout(loop, 500);
  }
};
loop();

This code results in:

$ node examples/unnamed.js
not done yet...
not done yet...
not done yet...
not done yet...
all done!

named

var DoneCriteria = require('../lib/done-criteria')
  , exit = false;

var doneCriteria = new DoneCriteria(
    {
        'eat': [
            ['get ingredients', 'cook']
          , function(name) { console.log('%s is done', name); }
        ]
      , 'clean up': [
            ['put dishes into sink', 'wash them']
          , function(name) { console.log('%s is done', name); }
        ]
    }
  , function(error) {
      console.log('all done!');
      exit = true;
    }
);

setTimeout(function() { doneCriteria.done('eat', 'cook'); }, 200);
setTimeout(function() { doneCriteria.done('eat', 'get ingredients'); }, 600);

setTimeout(function() { doneCriteria.done('clean up', 'put dishes into sink'); }, 400);
setTimeout(function() { doneCriteria.done('clean up', 'wash them'); }, 1000);

var loop = function() {
  if (exit) {
    process.exit(0);
  } else {
    console.log('not done yet...');
    setTimeout(loop, 500);
  }
};
loop();

This code will result in:

$ node examples/named.js
not done yet...
not done yet...
eat is done
clean up is done
all done!

count

var DoneCriteria = require('../lib/done-criteria')
  , exit = false;

var doneCriteria = new DoneCriteria(3, function(error) {
  console.log('all done!');
  exit = true;
});

setTimeout(function() { doneCriteria.done(); }, 200);
setTimeout(function() { doneCriteria.done(); }, 400);
setTimeout(function() { doneCriteria.done(); }, 600);

var loop = function() {
  if (exit) {
    process.exit(0);
  } else {
    console.log('not done yet...');
    setTimeout(loop, 500);
  }
};
loop();

This code will result in:

$ node examples/count.js
not done yet...
not done yet...
all done!

License

MIT