Javascript 产量显然不适合猫鼬
我试图使用Javascript 产量显然不适合猫鼬,javascript,node.js,mongodb,ecmascript-6,Javascript,Node.js,Mongodb,Ecmascript 6,我试图使用es6
es6
'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;
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();
})
我的模块规格是:
'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;
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();
})
当我运行测试时,输出如下:
{}
we'r here(?)
{}
与JS+ES6相关的人可以帮助我。我的最后一个方法是对我的函数返回一个承诺,但这对我的职责没有帮助,我真的需要做这项工作
PS:可能是因为我的语法不是最好的(可能我使用的不是ES6的最佳实践),也可能是因为我的英语水平(可能是一些拼写错误)。因此,感谢您阅读此。如果您使用的是NodeJS,那么在某个时候需要使用Async/promissions。现在,您只需
生成一个未执行的查询。或者,您正在寻找类似的产品。我认为您不了解yield
的功能。您无法将任何异步转换为同步(相信我,您甚至不想)。首先了解承诺和ES7异步/等待。那你就可以使用发电机了。