Created
June 26, 2018 16:35
-
-
Save nitish24p/52a1049d66aeec239543e2a376c2d426 to your computer and use it in GitHub Desktop.
User Model
This file contains 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 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