Skip to content

Instantly share code, notes, and snippets.

@biswanaths
Last active October 4, 2018 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biswanaths/b8999c9e4e5a9e979dcde061074528f6 to your computer and use it in GitHub Desktop.
Save biswanaths/b8999c9e4e5a9e979dcde061074528f6 to your computer and use it in GitHub Desktop.
const localdb = require('node-persist')
const when = require('when');
class QueuedStorage {
constructor() {
this.storage = localdb.create({ ttl: true, logging: false})
}
async init() {
await this.storage.init();
}
async getItem(key) {
this.current = when(this.current,
() => { return this.storage.getItem(key)},
() => { return this.storage.getItem(key)});
return this.current;
}
async setItem(key, value) {
this.current = when(this.current,
() => { return this.storage.setItem(key,value)},
() => { return this.storage.setItem(key,value)});
return this.current;
}
};
//const storage = new QueuedStorage();
const storage = localdb.create({ ttl: true, logging: true })
async function main () {
await storage.init()
try {
const key = 'DSF-AS-558-DDDF';
const fs = new Array(10000)
.fill(0)
.map((e,i) => function() {
if(i%2) return storage.getItem(key)
else return storage.setItem(key,i)}());
Promise.all(fs).then(values => console.log(values));
} catch (err) {
console.error(err)
}
}
main().catch(err => {
console.error(err)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment