Skip to content

Instantly share code, notes, and snippets.

@Software-Engineering24
Forked from sdesalas/util.promisify.js
Created November 23, 2022 18:20
Show Gist options
  • Save Software-Engineering24/565f96f459a0fc17189436b8cf4e5dc2 to your computer and use it in GitHub Desktop.
Save Software-Engineering24/565f96f459a0fc17189436b8cf4e5dc2 to your computer and use it in GitHub Desktop.
const util = require('util');
setTimeout(async () => {
db.getData = util.promisify(db.getData);
db.processData = util.promisify(db.processData);
db.saveData = util.promisify(db.saveData);
try {
const data = await db.getData();
data.a = 'test';
data.b = 1111;
await db.processData(data);
await db.saveData();
console.log('finished');
} catch (err) {
console.log(err);
}
}, 0);
// MOCK DB ---------------------------------------------
const db = {
getData: function(callback) {
this.hello = 'world';
const err = null;
callback(err, {...this});
},
processData: function(data, callback) {
Object.assign(this, data);
this.processed = true;
const err = null;
callback(err, {...this});
},
saveData: function(callback) {
const err = null;
this.saved = true;
callback(err, {...this});
},
};
// OUTDATED LIBRARY -----------------------------
db.getData((err, data)=>{
if (err) {
console.log(err);
return;
}
data.a = 'test';
data.b = 1111;
db.processData(data, (err, data)=>{
if (err) {
console.log(err);
return;
}
db.saveData((err, data)=>{
if (err) {
console.log(err);
return;
}
console.log('finished');
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment