In my JavaScript learning journey, I encountered a complex problem that lasted more than 24 hours to research and try several published solutions, but unfortunately, I did not succeed in solving my problem. Which tempted me to write this reply to solve this complex problem for me!
class db{
async findOne(search){
try {
const doc = this.collection.doc(search).get();
return get.data()
} catch (error) {
console.error(Error(red(error)).message);
process.exit(1)
}
}
}
Output
Promise { <pending> }
What I really want is for the output to be done without using then
and be like the following output:
{
name:'Johan',
age:'15',
}
CodePudding user response:
Is this this.collection.doc(search).get();
synchronous, i guess it's not.
so, const doc = await this.collection.doc(search).get();
this will solve your problem.
CodePudding user response:
You have to use await it wait on the specifix line until it finish Here you go!!
const doc = await this.collection.doc(search).get();
CodePudding user response:
If you are getting arrays of promises you can resolve them by using promise.All([Array of promises]);
Promise.all([promise1, promise2, promise3]).then((values) => {
console.log(values);
});
for more information, you can go to MMDN