Created
February 2, 2019 21:43
-
-
Save franck34/c08eca6379c60f6694468341a7442e45 to your computer and use it in GitHub Desktop.
nodejs async await for noob like me
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 { promisify } = require("util"); | |
function Foo() { | |
this.one = promisify( | |
function(value, callback) { | |
setTimeout(() => { | |
console.log("one", value); | |
callback(null, true); | |
}, 500); | |
} | |
); | |
this.two = promisify( | |
function(value, callback) { | |
setTimeout(() => { | |
console.log("two", value); | |
callback(null, true); | |
}, 500); | |
} | |
); | |
this.err = promisify( | |
function(value, callback) { | |
callback("hooo shiiit"); | |
} | |
); | |
return this; | |
} | |
async function go() { | |
let foo = new Foo(); | |
try { | |
await foo.one("myone"); | |
await foo.two("mytwo"); | |
await foo.err("hu"); | |
} catch(e) { | |
console.log("error: ",e); | |
} | |
} | |
go(); |
Author
franck34
commented
Feb 2, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment