Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* if we're doing function with promises, we must return it */
- function findUser (login, password) {
- return db.models.user.find({ // here we MUST return promise
- login: login,
- password: password
- });
- }
- findUser('example', 'examplepw').then(function (user) {
- // Do something with user.
- });
- /* So if 'return' missed */
- function findUserWrong (login, password) {
- db.models.user.find({
- login: login,
- password: password
- });
- } // if not 'return' specified, function returns 'undefined'
- findUserWrong('example', 'examplepw').then(function (user) { // function throws an error, because 'undefined' haven't any functions
- // Do something with user.
- });
- // even if we will write in async context
- let user = await findUserWrong('example', 'examplepw'); // user will be undefined, because function returns 'undefined'
- /* Complete example with ES6 */
- function findUser (login, password) {
- return db.models.user.find({
- login,
- password
- });
- }
- findUser(login, password)
- .then(user => {
- // Do something...
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement