async and await :
1). async and await must be used together, except JS Modules and Chrome Dev Tools
2). Async and await only affect the promise receiver
3). You can await any function that returns a promise
4). Any function can be converted into an async function
5). All async functions return a promise object by default.
Example_1:
async function getData(){
const res= await fetch("url")
const data = await res.json()
return data;
}
console.log((await getData()))
example_2: customisation
async function getData(){
const res= await fetch("url")
return await res.json()
}
**example_**3:
async function getData() {
return (res = await fetch("url")).json();
}
async: Async is a keyword that is used before a function to create an async function.
An async function will always return a promise, it can be either a promise object or a value like a string or number, etc. Suppose if you return the normal value, then it basically takes this value and wraps it inside a promise object, and it will return it.
async function getData() {
return { name: "Shivaji" };
}
const promiseData = getData();
promiseData.then((res) => console.log(res)); //access the returned value