I'm using co and mongoose and I'd like to my asynchronous code look little more "synchronously" and - as far as I read - co library allows me use data from one yielded promise in another to avoid callback hell. It seems to work with mongoose save (even if I do multiple saves), but it does nothing with promises returned from queries like find() or findOne(). Why is that? What can I do to fix it?
Here'a a piece of my code:
co(function *() {
let unhashedPassword = Math.random().toString(36);
let passed = {
username: 'T1',
password: bcrypt.hashSync(unhashedPassword)
};
let saved = yield new test_model(passed).save();
console.log("saved: " + saved);
let found = yield test_model.findOne({username: saved.username}).exec();
console.log("found" + found);
});
And the output:
saved: { _id: 57606dcf0f2378d41c355acd,
password: '...',
username: 'T1',
__v: 0 }
Process finished with exit code 0