Created
October 24, 2016 15:05
-
-
Save ktrysmt/70df34dd71fabbf28393a4daf5b48882 to your computer and use it in GitHub Desktop.
A sample code of async/await by ES7
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 panic = () => { | |
return new Promise((resolve) => { | |
throw new Error("throw.. ") | |
}) | |
} | |
const sleep = () => { | |
return new Promise((resolve) => { | |
setTimeout(function() { | |
console.log("fin.") | |
resolve("sleeped.") | |
}, 1000) | |
}) | |
} | |
const finalTask = () => { | |
return new Promise((resolve) => { | |
console.log("finalTask") | |
resolve("end.") | |
}) | |
} | |
(async () => { | |
const r1 = await sleep() | |
const rp = await panic() | |
const r2 = await finalTask() | |
console.log(["Result:", r1, r2, rp]) | |
})() | |
.catch( err => { | |
console.log("is error. -> " + err) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Above the code includes practices about;