Running arbitrary number of mongo queries in nodejs
I'm pretty sure Mongo has native Promise support, that in conjunction with async/await would really tidy this up

async getFieldsFromCollection (collection, socket) {
  try {
    const db = await MongoClient.connect(MAIN_DB);
    const c = db.collection(collection);
    const doc = await c.findOne({}, {});
    if (!doc) throw new Error('document is null');

    const alphaFields = Object.keys(doc).filter(k =&gt; /^[A-Z]/.test(doc[k]));
    const alphaFieldsValues = {};
    const subQueries = alphaFields.map(async (f) =&gt; {
      const docs = await c.distinct(f);
      alphaFieldsValues[f] = docs;
    });
    await Promise.all(subQueries);
    return socket.emit('getFieldsFromCollectionReply', {
      alphaFields,
      alphaFieldsValues
    });
  } catch (e) {
    return log(e);
  }
}