Java script callback promises and async await function

Callback functions

A function which pass as an argument to other function and call inside that function is called callback function.

Example

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> </body> </html> <script> // data var groups=[ { "id":1, "name":"HP" }, { "id":2, "name":"DELL" } ]; var products=[ { "id":1, "group_id":1, "name":"HP Laptop" }, { "id":2, "group_id":1, "name":"HP Mouse" }, { "id":3, "group_id":2, "name":"Dell laptop" } ]; function loadGroup(id, callback){ let group=groups.find(item=>{ return item.id == id; }) callback( group ); } function loadProducts(id, callback){ let product=products.find(item=>{ return item.group_id == id; }) callback(product); } loadGroup(1, function(group){ console.log("Loaded at callback for group"); console.log(group); loadProducts(group.id, function(product){ console.log("Loaded at callback for product") console.log(product); }) }); </script>
© 2016 - 2022, All Rights are Reserved.