Last active
July 23, 2018 17:22
-
-
Save obbaeiei/1c4a0eb29bc5cc79b7132800a76ee8d8 to your computer and use it in GitHub Desktop.
Async/await example 03
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
function main() { | |
console.log(1) // ปริ้น 1 | |
delay(1000) // รอ 1 วินาที ถึงจะเรียกฟังชั่นบรรทัดที่ 4 | |
.then(()=> { | |
console.log(2) // ปริ้น 2 | |
return delay(2000) // รอ 2 วิ ค่อยเรียกบรรทัดที่ 8 | |
}) | |
.then(()=>{ | |
console.log(3) | |
}) | |
console.log(1.5) // ปริ้นทันทีต่อจาก 1 ปริ้นก่อน 2 | |
// code จะถูกผสมด้วย async | |
// และ sync ทำให้อ่านยาก | |
} | |
function delay(ms) { | |
return new Promise((resolve)=>{ | |
setTimeout(()=>{ | |
resolve() | |
}, ms) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment