Skip to content

Instantly share code, notes, and snippets.

@thomasmichaelwallace
Last active December 4, 2018 10:02
Show Gist options
  • Save thomasmichaelwallace/cd77294cac8b6b4ecf054bf2a42fe7fc to your computer and use it in GitHub Desktop.
Save thomasmichaelwallace/cd77294cac8b6b4ecf054bf2a42fe7fc to your computer and use it in GitHub Desktop.
How to make a singleton promise.
// a connection singleton.
let dbConnectionPromise;
function getDb() {
if (dbConnectionPromise) {
return dbConnectionPromise;
}
dbConnectionPromise = DbClient.connect(options);
return dbConnectionPromise;
}
// partition worker using singleton.
const writeSomeTheThings = async (someThings) => {
const db = await getDb();
await writeThings(db, someThings);
};
// parallel writer.
const writeAllOfTheThings = (allThings) =>
Promise.all(getSome(allThings).map(writeSomeTheThings));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment