Posts

Showing posts with the label async-await

Javascript - TypeError: x is not a function at done

Javascript - TypeError: x is not a function at done I am building my own browserside javascript sdk, using webpack and node. I have built a async/await document function, which simply submits a document to an api. This function could be called twice in the browser (if there is a second image) However on the second document function call i get the following error. TypeError: hsp.document is not a function at done index.html <script> hsp = new HSP(); // function called on button click async function done() { try { const doc = await hsp.document(transaction, token, url, this.frontBase64); console.log(doc); // If second image, submit it also. if(this.backBase64) { const doc = await hsp.document(transaction, token, url, this.backBase64); } } catch (er) { console.log(er); } } </script> sdk.js async document(transaction, token, url, doc) { try { this.document = await this.api.submit...

.Net Core Queue Background Tasks

.Net Core Queue Background Tasks Slender answered my original question about what happens to fire and forget, after the HTTP Response is sent, but Now I'm left with the question how to properly queue background tasks EDIT As we all know Async void is generally bad, except for in the case when it comes to event handlers, I would like to execute some background logic without have to have the client wait. My original Idea was to use Fire and Forget Say I have an event: public event EventHandler LongRunningTask; And then someone subscribes a fire and forget task: LongRunningTask += async(s, e) => { await LongNetworkOperation;}; the web api method is call: [HttpGet] public async IActionResult GetTask() { LongRunningTask?.Invoke(this, EventArgs.Empty); return Ok(); } But If I do this my long running task isn't guaranteed to finish, How can I handle running background task without affect the time the time it take to make my request (e.g I don't want to wait for the t...

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...