Advertisement
Guest User

Untitled

a guest
Aug 19th, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function synchronizedFor (_start, _end, _iteration) {
  2.     return new Promise (function (resolveAll, rejectAll) {
  3.         var currentIndex = _start;
  4.         var isPromisInProgres = false;
  5.  
  6.         var interval = setInterval (function () {
  7.             if (!isPromisInProgres) {
  8.                 if (currentIndex >= _end) {
  9.                     clearInterval (interval);
  10.                     resolveAll ();
  11.                 }
  12.                 else {
  13.                     new Promise (function (resolve, reject) {
  14.                         isPromisInProgres = true;
  15.                         _iteration (currentIndex, resolve);
  16.                     }).then (function () {
  17.                         isPromisInProgres = false;
  18.                         ++currentIndex;
  19.                     });
  20.                 }
  21.             }
  22.         }, 50);
  23.     });
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement