Skip to content

Instantly share code, notes, and snippets.

@nitish24p nitish24p/User.js
Created Jun 26, 2018

Embed
What would you like to do?
User Model
const user = {
id: 1,
username: 'Nitish',
age: 25,
profession: 'dev'
}
const error = new TypeError('Something broke in the db');
class User {
static findUser (postBody, callback) {
const doesExist = Math.random() > 0.5 ? true : false;
const shouldThrowError = Math.random() > 0.8 ? true : false;
setTimeout(() => {
const data = doesExist ? user : null;
const err = shouldThrowError ? error : null;
callback(err, data);
}, 1000);
}
addUser(data, callback) {
return this.save(data, callback);
}
save(obj, callback) {
const shouldThrowError = Math.random > 0.8 ? true : false
setTimeout(() => {
const data = obj;
const err = shouldThrowError ? error : null;
callback(err, data);
}, 1000);
}
update(obj, callback) {
const shouldThrowError = Math.random > 0.8 ? true : false
setTimeout(() => {
const data = obj;
const err = shouldThrowError ? error : null;
callback(err, data);
}, 1000);
}
static findUserPromise(postBody) {
return new Promise((resolve, reject) => {
const doesExist = Math.random() > 0.5 ? true : false;
const shouldThrowError = Math.random() > 0.8 ? true : false;
setTimeout(() => {
const data = doesExist ? user : null;
const err = shouldThrowError ? error : null;
if (err) {
return reject(err)
} else {
return resolve(data);
}
}, 1000);
})
}
savePromise(obj) {
return new Promise((resolve, reject) => {
const shouldThrowError = Math.random > 0.8 ? true : false
setTimeout(() => {
const data = obj;
const err = shouldThrowError ? error : null;
if (err) {
return reject(err)
} else {
return resolve(data);
}
}, 1000);
});
}
updatePromise(obj) {
return new Promise((resolve, reject) => {
const shouldThrowError = Math.random > 0.8 ? true : false
setTimeout(() => {
const data = obj;
const err = shouldThrowError ? error : null;
if (err) {
return reject(err)
} else {
return resolve(data);
}
}, 1000);
});
}
};
module.exports = User;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.