<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>async await fetch example</title>
</head>
<body>
</body>
<script>
// let url='https://jsonplaceholder.typicode.com/users'
// let url='https://randomuser.me/api/?results=1000'
Example:1
___________
let url='https://api.github.com/users'
async function GetResult(){
let response = await fetch(url);
let data = await response.json();
return data;
}
GetResult()
.then(data => console.log(data))
Example:2
__________
async function fetchAsync () {
let data = await (await fetch('https://api.github.com/users')).json();
return data;
}
fetchAsync()
.then(data => console.log(data))
</script>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>async await fetch example</title>
</head>
<body>
</body>
<script>
// let url='https://jsonplaceholder.typicode.com/users'
// let url='https://randomuser.me/api/?results=1000'
Example:1
___________
let url='https://api.github.com/users'
async function GetResult(){
let response = await fetch(url);
let data = await response.json();
return data;
}
GetResult()
.then(data => console.log(data))
Example:2
__________
async function fetchAsync () {
let data = await (await fetch('https://api.github.com/users')).json();
return data;
}
fetchAsync()
.then(data => console.log(data))
</script>
</html>
0 Comments