Skip to content

Commit

Permalink
16 Ajax and Restful Api > Async and Await > add
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMadWill committed Jan 3, 2022
1 parent f256b20 commit 85571cd
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
3 changes: 2 additions & 1 deletion 16_Ajax_Restful_Api/HTML/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ <h1>JavaScript</h1>
<!-- <script src="../Js/72_16_6_AsyncJavaScript.js"></script> -->
<!-- <script src="../Js/73_16_7_Promise.js"></script> -->
<!-- <script src="../Js/74_16_8_Promise_using.js"></script> -->
<script src="../Js/75_16_9_Fetch_Api.js"></script>
<!-- <script src="../Js/75_16_9_Fetch_Api.js"></script> -->
<script src="../Js/76_16_10_Asinc_and_Await.js"></script>

</body>
</html>
48 changes: 48 additions & 0 deletions 16_Ajax_Restful_Api/Js/76_16_10_Asinc_and_Await.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@


async function AsyncFunction(){
return "Not need Promise"
}


// AsyncFunction().then(res => console.log(res))

let isError = true;

function GetCategory(){
return new Promise((resolve, reject)=>{
setTimeout(()=>{
if (!isError){
resolve("Phone")
}else{
reject("Some Error ")
}
},1000)
})
}


function GetProduct(category){
return new Promise((resolve, reject)=>{
setTimeout(()=>{
resolve(`5 prodacts in ${category}`)
},1000)
})
}


GetCategory().then(GetProduct).then(res =>console.log(res)).catch(err => console.log(err))

async function GetProducts(){

try{
let category = await GetCategory();
let result = await GetProduct(category);
console.log(result)
}catch(error){
console.log(error)
}

}

GetProducts()

0 comments on commit 85571cd

Please sign in to comment.