Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/idindrakusuma/learning-async-await-node
https://github.com/idindrakusuma/learning-async-await-node
Last synced: 6 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/idindrakusuma/learning-async-await-node
- Owner: idindrakusuma
- Created: 2018-08-10T15:03:08.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-08-11T03:50:54.000Z (over 6 years ago)
- Last Synced: 2024-12-22T17:15:55.945Z (12 days ago)
- Language: JavaScript
- Size: 1000 Bytes
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# rangkuman belajar async await
`Async await` merupakan syntax baru pada javascipt. Syntax ini diperkenalkan di ES6. untuk menjalankanyapun tidak semua versi NodeJS bisa. Minimal harus versi `NodeJS > 7`.
`async await` berfungsi sebagai alternatif dari penggunaan promise yang codenya lumayan panjang. pakai async await jadi lebih simple. contoh pemanggilan fungsi async await
```javascript
/* get status using normal promise */
const getStatus = (userId) => {
let user;
return getUser(userId)
.then(tempUser => {
user = tempUser;
return getGrades(user.schoolId)
})
.then(grades => {
let average = 0;
if(grades.length > 0) {
average = grades.map(grade => grade.grade).reduce((a, b) => a + b) / grades.length;
}return `${user.name} has a ${average}% in the class.`
})
}
``````javascript
/* using async await */
const getStatusAsync = async (userId) => {
const user = await getUser(userId);
const grades = await getGrades(user.schoolId);
let average = 0;
if(grades.length > 0) {
average = grades.map((grade) => grade.grade).reduce((a, b) => a + b) / grades.length;
}
return `${user.name} has a ${average}% in the class.`
}
```