Skip to content

Instantly share code, notes, and snippets.

@ktrysmt
Created October 24, 2016 15:05
Show Gist options
  • Save ktrysmt/70df34dd71fabbf28393a4daf5b48882 to your computer and use it in GitHub Desktop.
Save ktrysmt/70df34dd71fabbf28393a4daf5b48882 to your computer and use it in GitHub Desktop.
A sample code of async/await by ES7
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)
})
@ktrysmt
Copy link
Author

ktrysmt commented Oct 24, 2016

Above the code includes practices about;

  • Use setTimeout to make sleep-function
  • How to throw error
  • and Error handling
  • Get responses for each tasks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment