Hello all,I've been working on using ASYNC and AWAIT, and for some things it seems to be working just fine. However I'm running into a specific problem I don't understand. In the code below I'm saving user data into MongoDB via Mongoose.This code block works fine:const addUser = async (firstName, lastName, email, password) => {return new Promise((resolve, reject) => {const user = new Users({firstName: firstName,lastName: lastName,email: email,password: password,isPublic: true});const result = user.save();if (result) {resolve(result);} else {reject(err);}});};This code block does not:const addUser = async (firstName, lastName, email, password) => {return new Promise((resolve, reject) => {const user = new Users({firstName: firstName,lastName: lastName,email: email,password: password,isPublic: true});try {return await user.save()} catch (error) {return error}};I am under the impression that when you 'await' the promise, it's handing the resolution for you. Is this not correct? I know the try / catch sequence is handled, but do you still need to resolve or reject the promise? It's looking like you still do. Also is there a "better way" than a simple if statement to check the promise's resolved condition?Kind regards
Submitted December 21, 2018 at 02:12PM by sma92878
No comments:
Post a Comment