daily pastebin goal
75%
SHARE
TWEET

Untitled

a guest Nov 24th, 2017 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Promise.joinPooledTasks = (worker, poolSize) => {
  2.   return new Promise((resolve, reject) => {
  3.     var results = [];
  4.     var numberOfRunningTasks = 0;
  5.     var didEnqueAllTasks = false;
  6.     var nextTaskIndex = 0;
  7.    
  8.     var enqueNextTaskIfPossible = () => {
  9.       if (numberOfRunningTasks < poolSize) {
  10.         var nextTask = worker(nextTaskIndex);
  11.         if (nextTask === null) {
  12.           didEnqueAllTasks = true;
  13.         }
  14.         else {
  15.           results.push(0);
  16.           enqueTask(nextTask, nextTaskIndex);
  17.           nextTaskIndex += 1;
  18.           if (numberOfRunningTasks < poolSize) {
  19.             enqueNextTaskIfPossible();
  20.           }
  21.         }
  22.       }
  23.     };
  24.    
  25.     var resolveIfNeeded = () => {
  26.       var shouldResolve = didEnqueAllTasks && numberOfRunningTasks === 0;
  27.       if (shouldResolve) {
  28.             resolve(results);
  29.       }
  30.       else if (!didEnqueAllTasks) {
  31.         enqueNextTaskIfPossible();
  32.       }
  33.       return shouldResolve;
  34.     };
  35.    
  36.     var enqueTask = (promise, index) => {
  37.       var isPromise = typeof promise.then == 'function';
  38.         if (isPromise) {
  39.           numberOfRunningTasks += 1;
  40.           promise.then(result => {
  41.             results[index] = { result : result };
  42.             numberOfRunningTasks -= 1;
  43.             resolveIfNeeded();
  44.           }, err => {
  45.             results[index] = { error : err };
  46.             numberOfRunningTasks -= 1;
  47.             resolveIfNeeded();
  48.           });
  49.         }
  50.         else {
  51.           results[index] = { result : promise };
  52.           resolveIfNeeded();
  53.         }
  54.     };
  55.    
  56.     enqueNextTaskIfPossible();
  57.   });
  58. }
  59.  
  60.  
  61. //Testing code
  62.  
  63. function getRandomInt(min, max) {
  64.   min = Math.ceil(min);
  65.   max = Math.floor(max);
  66.   return Math.floor(Math.random() * (max - min)) + min;
  67. }
  68.  
  69. var worker = index => {
  70.   if (index > 20) return null;
  71.   return new Promise((resolve, reject) => {
  72.       setTimeout(() => {
  73.         if (Math.random() > 0.5) {
  74.           console.log(`${index} >> success`);
  75.           resolve(index)
  76.         }
  77.         else {
  78.           console.log(`${index} >> failure`);
  79.           reject(index);
  80.         }
  81.     }, getRandomInt(1000, 2250));
  82.   });
  83. };
  84.  
  85. Promise.joinPooledTasks(worker, 3).then(results => {
  86.   console.log(`${results.length} promises ended`);
  87.   for (var i=0; i < results.length; i++) {
  88.     var result = results[i];
  89.     if (result.result) {
  90.       console.log(`${i} >> success: ${result.result}`);
  91.     }
  92.     else {
  93.       console.log(`${i} >> failure: ${result.error}`);
  94.     }
  95.   }
  96. }, err => { console.log(err); } );
RAW Paste Data
Top