Learning to Keep my Promises with Mongoose.js
Micah Bales
11

First, I think your code has a bug in it.

Key.findOne({ myKey })
.then((myKey) => {
debugger;
return key.attribute;
});

I think you mean .then((key) => {, not .then((myKey) => {.

More relevantly, though: why the continuation passing style (CPS) on save? You usually don’t mix and match Promises with CPS — they solve the same problem, and going both directions is confusing. Usually, libraries (eg: localforage) automatically return Promises while also taking CPS. If that’s what record.save() does, then you can just rewrite the code as:

// pass the new attribute from attributePromise
attributePromise.then((attribute) =>
new Record({ attribute }).save()
).catch(console.log.bind(console))

I’d also suggest you use console.exception for printing out errors.

If you ever need to convert CPS to Promises, take a look at Bluebird.promiseify.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.