Posts

Showing posts with the label promise

Await not awaiting

Await not awaiting I am getting started with both MathJax and using await where I am formatting a series of lines which can contain math. The math is denoted by the delimiters $...$ . $...$ Problem: I need to wait for MathJax to complete its conversion (I do get some sort of html output) however the conversion is not waiting and the rest of format() is executing. Part of my code is modeled after the answer given in this question. format() function MJ(math) { // as per the documentation, this returns a promise if no callback is set return mathjax.typeset({ math: math, format: "inline-TeX", html: true, }); } async function convert(line) { var re = /$(.*?)$/; var match = re.exec(line)[0]; var math = match.slice(1, -1); // ORIGINAL CODE // let result = await MJ(math).then(function(data){return line.replace(match,data.html);}); // return result; let result = await MJ(math); console.log(`MJ is ready: ${result.htm...

How to properly implement mongodb async/await inside a promise?

How to properly implement mongodb async/await inside a promise? I've read that having an async inside a Promise is anti-pattern for async/await. The code below works, but I am curious how else to achieve the same result without having async in Promise . Promise async Promise If I remove it, the linter would tell how I can't use await in my mongodb query. If I remove the await in the mongodb query, then it wouldn't wait for the result. export const getEmployees = (companyId) => { return new Promise(async (resolve, reject) => { const employees = await Employees.find( { companyId }, ); // other logic here... resolve({ employees, }); }); Thanks. 2 Answers 2 async functions automatically return Promise s already, which resolve with whatever expression is eventually return ed. Simply make getEmployees an async function: async Promise return getEmp...