Skip to content

Instantly share code, notes, and snippets.

@chonlatee
Last active February 1, 2017 08:29
Show Gist options
  • Save chonlatee/b2b8bda9b1ef8b3558c8c1951dec0fbb to your computer and use it in GitHub Desktop.
Save chonlatee/b2b8bda9b1ef8b3558c8c1951dec0fbb to your computer and use it in GitHub Desktop.
just show about how to use promise and async await in same file
// need to use babel
// npm install babel-cli
// babel-node file.js
const a = (x) => {
return new Promise(resolve => {
setTimeout(() => {
resolve(x)
}, 3000)
})
}
const b = (x) => {
return new Promise(resolve => {
setTimeout(() => {
resolve(x)
}, 2000)
})
}
const c = (x) => {
return new Promise(resolve => {
setTimeout(() => {
resolve(x)
}, 1000)
})
}
const d = (x) => {
return new Promise(resolve => {
setTimeout(() => {
resolve(x)
}, 500)
})
}
const foo = async () => {
console.log(await a(3))
console.log(await b(4))
}
foo()
c(1)
.then(val => console.log(val))
.then(() => d(2))
.then(val => console.log(val))
// output: 1 2 3 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment