Last active
February 1, 2017 08:29
-
-
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
This file contains hidden or 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
// 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