Given below is a simple example on how to use async, await when using javascript fetch.
----------------------------------------
//create fetch
let reqPromise = fetch('https://jsonplaceholder.typicode.com/posts/1');
async function getData(){
//await for 'fetch' response and then await to convert response to
'json'
let response = await(await reqPromise).json();
console.log(response);
}
//call async function
getData();----------------------------------------
You can see it working here - https://codepen.io/deebujacob/pen/BYveQm
No comments:
Post a Comment