Parse Cloud Code does not support setTimeout

I am using Parse cloud code to write a little crawl function. However the API has a 1 request/second limit. So I have to delay after each API request.

The problem is Parse Cloud Code does not support setTimtout (They claim that this will keep many long-live threads alive)

Solution:
Using promise

var delayUntil;
var delayPromise;

var _delay = function () {
   if (Date.now() >= delayUntil) {
      delayPromise.resolve();
      return;
   } else {
      process.nextTick(_delay);
   }
 }

var delay = function(delayTime) {
  delayUntil = Date.now() + delayTime;
  delayPromise = new Parse.Promise();
  _delay();
  return delayPromise;
  };

How to use :

var delayFoo = function(){foo()};
delay(1000).then(delayFoo);
comments powered by Disqus