Skip to content

Instantly share code, notes, and snippets.

@kenanhancer
Created April 8, 2018 15:49
Show Gist options
  • Save kenanhancer/c8d5385b167a06148e03036e52ab1812 to your computer and use it in GitHub Desktop.
Save kenanhancer/c8d5385b167a06148e03036e52ab1812 to your computer and use it in GitHub Desktop.
PromiseTutorial2 created by kenanhancer - https://repl.it/@kenanhancer/PromiseTutorial2
const delay = duration => new Promise(resolve => setTimeout(resolve, duration));
const uniqueId = () =>
Math.random().toString(36).substring(2) + new Date().getTime().toString(36);
class Person {
constructor(firstName, lastName) {
this.PersonId = uniqueId();
this.firstName = firstName;
this.lastName = lastName;
this.created = Date.now();
this.updated = this.created;
this.isDeleted = false;
}
getFullName() {
return this.firstName + ' ' + this.lastName;
}
}
const getPersonsAsync = async () => {
await delay(1000);
return [
new Person('Kenan', 'Hancer'),
new Person('Enes', 'Cakir'),
new Person('Hakan', 'Duman'),
new Person('Hasan', 'Oz'),
new Person('Kerim', 'Tekin'),
new Person('Nida', 'Demir'),
new Person('Firat', 'Kamis'),
];
};
const getPersons = callback => {
getPersonsAsync()
.then(persons => {
if (callback.length == 1) {
callback(persons);
} else {
callback(null, persons);
}
})
.catch(ex => {
if (callback.length == 1) {
return callback(ex);
} else {
return callback(ex, null);
}
});
};
getPersons((err, result) => {
for (let i = 0; i < result.length; i++) {
console.log(result[i]);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment