example.js
async function main () {
  console.log(`\n\n[Single step producer]`);
  const workers1 = [
    new Producer(100),
    new Producer(100),
    new Producer(100),
    new Producer(100),
    new Producer(100),
    new Consumer(200),
    new Consumer(300),
  ];
  await Promise.all(workers1.map(x => x.work()));

  console.log(`\n\n[Efficient producer]`);
  const workers2 = [
    new EfficientProducer(10),
    new Consumer(10),
    new Consumer(10),
    new Consumer(10),
    new Consumer(10),
    new Consumer(10),
    new Consumer(10),
    new Consumer(20),
    new Consumer(20),
  ];
  await Promise.all(workers2.map(x => x.work()));

  console.log(`\n\n[RejectAll() API test]`);
  const p = new Consumer(1).work();
  stock.notempty.rejectAll();
  p.then(
    () => console.error(`[ERROR] This promise should not resolve.`),
    err => console.log(`Successfully received a rejected Error - ${err.message}`)
  );
}
async function main () {
  console.log(`\n\n[Single step producer]`);
  const workers1 = [
    new Producer(100),
    new Producer(100),
    new Producer(100),
    new Producer(100),
    new Producer(100),
    new Consumer(200),
    new Consumer(300),
  ];
  await Promise.all(workers1.map(x => x.work()));

  console.log(`\n\n[Efficient producer]`);
  const workers2 = [
    new EfficientProducer(10),
    new Consumer(10),
    new Consumer(10),
    new Consumer(10),
    new Consumer(10),
    new Consumer(10),
    new Consumer(10),
    new Consumer(20),
    new Consumer(20),
  ];
  await Promise.all(workers2.map(x => x.work()));

  console.log(`\n\n[RejectAll() API test]`);
  const p = new Consumer(1).work();
  stock.notempty.rejectAll();
  p.then(
    () => console.error(`[ERROR] This promise should not resolve.`),
    err => console.log(`Successfully received a rejected Error - ${err.message}`)
  );
}
async function main ( ) { console . log ( ` \n \n ` ) ; const workers1 = [ new Producer ( 100 ) , new Producer ( 100 ) , new Producer ( 100 ) , new Producer ( 100 ) , new Producer ( 100 ) , new Consumer ( 200 ) , new Consumer ( 300 ) , ] ; await Promise . all ( workers1 . map ( x => x . work ( ) ) ) ; console . log ( ` \n \n ` ) ; const workers2 = [ new EfficientProducer ( 10 ) , new Consumer ( 10 ) , new Consumer ( 10 ) , new Consumer ( 10 ) , new Consumer ( 10 ) , new Consumer ( 10 ) , new Consumer ( 10 ) , new Consumer ( 20 ) , new Consumer ( 20 ) , ] ; await Promise . all ( workers2 . map ( x => x . work ( ) ) ) ; console . log ( ` \n \n ` ) ; const p = new Consumer ( 1 ) . work ( ) ; stock . notempty . rejectAll ( ) ; p . then ( ( ) => console . error ( ` ` ) , err => console . log ( ` ${ err . message } ` ) ) ; }
Async wrapped main function.
Async wrapped main function .