+ 1
Hello Solorians!! I am not understanding the concept of assync,await,etc. promises.anyone has better way to understand this?
5 odpowiedzi
+ 7
Watch this video for better understanding:
https://youtu.be/_8gHHBlbziw
if you get tired of using .then() there is another way (async await) You have to mark the function as async if you wanna use async await.
// .then
function getData() {
return axios.get("url").then(res => res.data)
}
getData().then(data => {
console.log(data);
})
// async await
async function getData() {
let res = await axios.get("url") // waiting for response instead of using .then
return res.data
}
async function showData() {
let data = await getData() // waiting fot data instead of using .then
console.log(data);
}
showData()
+ 5
instead of fetch im gonna use axios to make u understand.
axios.get("url").then(res => {
console.log(res.data)
})
In this example we are fetching some data from a url and logging it to the console, but what if we wanna make a function of it and whenever we call that function we should receive the data.
function getData() {
axios.get("url").then(res => {
return res.data
})
}
console.log(getData())
It wouldn't work coz axios call was asynchronous, we must return the promise instead.
function getData() {
return axios.get("url").then(res => res.data)
}
Now we can use a .then on our getData function!!
getData().then(data => {
console.log(data);
})
+ 5
My Asynchronous Tutorial Series, with applications :
https://www.youtube.com/playlist?list=PLkqwj9vc20pX36wThr2A6DZx4MVcJvnhe
+ 4
Sunayan Sarkar np :)
+ 1
Thanks a lot maf