Skip to content

Instantly share code, notes, and snippets.

@asduser
Created March 28, 2016 21:32
Show Gist options
  • Save asduser/3f15230294a7766d04ff to your computer and use it in GitHub Desktop.
Save asduser/3f15230294a7766d04ff to your computer and use it in GitHub Desktop.
Simple JavaScript callback.
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