I am using Mongoose and am in a situation in which I am looking up a customer from the database. If the customer exists, I will return a customerId. However, if the customer does not exist, I want to create them and then return the customer id. In either case, I want the promise to end with a customerID. Anyway, while I have seen in the documentation that is possible to instantly wrap a return value using "Promise.resolve", I am receiving an error. This feature is apparently useful when trying to make synchronous tasks more congruent with Node by wrapping their return values. Anyway, what is a better way to deal with this or return a promise? At the end of the day I want the code to return the customer object or fall through to a catch/error.
TypeError: Promise.resolve is not a constructor
Below is the code (I edited out some security things, but I think I didn't make any serious alterations).
Customer.findOne({email: email}).then((customer) => {
if (customer) {
return Promise.resolve(customer);
}
else {
return new Customer({email: email}).save();
}
}).then( function(customer) {...
This is really about Promises, not Mongoose or MongoDb. If you are in a situation in which you are inside of a promise and want to return a value to the chain, what do you do?