promise.race – Saves the Day!

Earlier, I discussed how we resolved the problem of connection leaking in an out-of-box thinking. Today I wish to share a different problem.

Our unexpected surge in GeoIP subscription caused a massive connection problem. While we were focusing on auto-sharding, we realized that it was not just too many connections, it was also saving of data as a transaction.

Problem in technical terms:

Connection is stuck somewhere – we don’t know where. Client is expecting a response for PixSpy – it ain’t giving!


RACE! – Lets race it down, whosoever wins get the output.

const itemOperation = 1000 * 0.1; //100 ms for our operation

const failedOnTimeout = new Promise((resolve,reject) => {
  return setTimeout(reject, itemOperation); //100 ms is more than enough for the operation
const saveMyData = new Promise(async(resolve,reject) => {
  // write your actual work here.
    return resolve(await Magic());
    return reject(e);

return Promise.race(failedOnTimeout,saveMyData);

Updated: 2017
PS: You will need babel here to compile code. While es6 native support it at least 2 years away.

Worth Sharing?


Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>