Take a look at https://github.com/kriskowal/asap/blob/master/asap.js#L75-L93 

I have to go sleep now, will have more time during the weekend if not sooner.