node package manager

Introducing npm Enterprise add-ons. Integrate third-party dev tools into npm…

slow

keep your pants on, async requests

Keep your pants on, javascript.

slow is smooth, smooth is fast.

it goes,

 npm install slow
slow=require('slow');
slow.walk( [1,2,3,4], random_wait, console.log);//results, in order 
 
function random_wait(i, callback){
  setTimeout(callbck(i), Math.random()*4000);
}

unlike the other woderful async libraries, this one lets you set the pace.

so you don't immediately do everything at once.

you can be safe with memory and respect external services.

the methods are:

explicitly set a pace, but respect a maximum current request rate (defaults to 10)

(it begins at this pace, but slows it down if callbacks begin to build-up)

  • slow.pace ( arr, fn, [options], callback ) //60bpm
  • slow.walk ( arr, fn, [options], callback ) //120bpm
  • slow.jog ( arr, fn, [options], callback ) //150bpm
  • slow.run ( arr, fn, [options], callback ) //180bpm
  • slow.heartbeat ( arr, fn, [options], callback ) //72bpm

do only a few things at a time. (only go as fast as your callback does)

  • slow.steady ( arr, fn, [options], callback ) //max=5
  • slow.handful ( arr, fn, [options], callback ) //max=3
  • slow.pocket ( arr, fn, [options], callback ) //max=7
  • slow.backpack ( arr, fn, [options], callback ) //max=15
  • slow.shovel ( arr, fn, [options], callback ) //max=35
{
 debug: true, //understand when the requests are being fired 
 verbose: true, //include the input in the results 
 monitor: function(r){console.log(r.length)}, //watch the results coming in in real-time 
 max: 10 //the most number of concurrent requests you're comfortable making 
}

but you can just ignore those tho

(2.8k)

<script src="https://raw.github.com/spencermountain/slow/master/slow.min.js"></script>
<script>
  slow.__walk__( [1,2,3,4,5,6,7], my_function, {max:3}, function(r){
    alert(r.join(''))
  })
  function my_function(q, callback){
    var x=Math.floor(Math.random()*2000)
    setTimeout(function(){callback("finished "+q+" in "+x+"ms")}, x)
  }
</script> 

if you're in a situation that needs fancy paramaters, wrap them up like this:

slow.steady( [1,2,3,4], whatev, console.log);
function whatev(i, callback){
  my_craziness(param1, param2, then_finally, a, callback) //works fine 
}

or if your function returns [errror, result] (i hate that)

slow.steady( [1,2,3,4], whatev, console.log);
function whatev(i, callback){
  my_craziness(i, function(err, result){
    callback(result||err);//la de da;) 
  })
}

MIT