-
-
Save Software-Engineering24/565f96f459a0fc17189436b8cf4e5dc2 to your computer and use it in GitHub Desktop.
This file contains 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
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