Last active
December 4, 2018 10:02
-
-
Save thomasmichaelwallace/cd77294cac8b6b4ecf054bf2a42fe7fc to your computer and use it in GitHub Desktop.
How to make a singleton promise.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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