Created
March 28, 2016 21:32
-
-
Save asduser/3f15230294a7766d04ff to your computer and use it in GitHub Desktop.
Simple JavaScript callback.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const users = [ | |
{Name: "Peter", Age: 30, Id: 1}, | |
{Name: "Bob", Age: 20, Id: 2}, | |
{Name: "Jack", Age: 25, Id: 3} | |
]; | |
class userManager { | |
constructor() { | |
this.getById = (id, callback) => { | |
let result = users.find( (u) => u.Id == id ); | |
if (result) { | |
callback(result); | |
} else { | |
callback(`There is no users with id ${id}`); | |
} | |
} | |
} | |
} | |
let um = new userManager(); | |
let test1 = um.getById(3, (response) => console.log(response.Name)); | |
let test2 = um.getById(5, (response) => console.log(response)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment