⚠️ This article was posted over a year go. The information might be outdated. ⚠️
Table of Contents
Async
Using async
simply implies that a function returns a promise.
Example
async function myAsync() {
return 27
}
myAsync().then(response => {
console.log(response)
})
//=> 27
Await
The await
operator is used to wait for a promise
. It can be used inside an async
block only. The keyword await
makes JavaScript wait until the promise
returns a result.
Example
async function myAsync() {
let promise = new Promise((resolve, reject) => {
setTimeout(() => resolve('done!'), 1000)
})
let result = await promise // wait until the promise resolves
alert(result) // "done!"
}
myAsync()
Exmple of fetching data
Example1
const request = async () => {
const response = await fetch('endpoint_url')
const json = await response.json()
return json
}
Example2
const fetchData = async () => {
const response = await axios.post('endpoint_url').catch(error => {
alert(error)
})
if (response) {
return response
}
}
Example3
async function getUserAsync(name) {
let response = await fetch(`https://api.github.com/users/${name}`)
let data = await response.json()
return data
}