I'm trying to make a synchronous query to mongodb with es6
yield
feature and when I'm calling my function on my test this apparently does not work.
my function code is the following:
'use strict';
let Notifications, lodash;
lodash = require('lodash')
Notifications = require('../../api/notifications/notifications.model');
function *syncQuery (email, kind, notificationIds) {
let ids, query, remains;
query = {
userEmail: email,
'notifications.kind' : kind
};
return yield Notifications.findOne(query, {}, {lean: true}, (err, usr) => {
let toNotify;
if (err) {
console.log('Error:', err);
reject(err);
}
toNotify = lodash.difference(notificationIds, usr.notifications.id);
return toNotify;
});
}
module.exports = syncQuery;
and my module spec is:
it('should be synchronous', done => {
let remains = syncQuery(n.user, 'budget', [9, 71, 24, 90, 5]);
console.log(remains);
remains.next();
console.log('we'r here(?)');
console.log(remains);
done();
})
when I run my test the output is the following:
{}
we'r here(?)
{}
Someone related with JS + ES6 who can help me please. My last approach was return a promise on my function but it does not helped me on my duty, I really need to do this job synchronous
PS: is probably that my syntax wasn't be the best(maybe I'm using not the best practices for ES6) and neither my english level (maybe some typo). so, thank even just for read this