Front end developer specialising in JavaScript and React. Experienced in all aspects of modern front end development. Passionate about making accessible, secure and performant software.
Here's one example of where you could have asynchronous function.
Currently, you're using a mock database which you set up yourself. It works synchronously. However, most databases you would use in production projects would take a long time to respond. They would work asynchronously. They would have some API that allows you to wait for it asynchronously. Most likely, they'll return a promise.
For example, in the helper_data.js file, your db.get function might look like this in a real codebase:
db.get=functionget(query,keys,subListing=global.listings){logger_.log({level:'info',message:'get'});constdataPromise=myDatabase.find('somequery');// myDatabase.find() would return a promisereturndataPromise;};
In terms of the database code you have right now, an easy thing to do might be to: keep most of your code the same, but return a promise. For example:
db.get=functionget(query,keys,subListing=global.listings){logger_.log({level:'info',message:'get'});returnPromise.resolve(lo.pick(lo(subListing).find(query),keys));// just wrap the data you're returning already into a promise with this line. This simulates what a real database package would return};
Then, in functions that use that code, you would need to handle those promises.
For example, in routes/index.js:
router.get('/tag/:tag',function(req,res,next){consttag=req.params.tag;db.toPublic()// returns a promise, so use .then to execute the remaining code.then(listings=>{returndb.fetchByTag(tag,listings);})// also returns a promise, so use .then again.then(listings=>{res.render('index',{title:tag,user:req.session.user,listings:listings,scripts:JSVersionMappings,});})});// or, alternative syntax with async await.// This is equivalent to the above, but does the .then behind the scenes// It doesn't work on older versions of Node, so be carefulrouter.get('/tag/:tag',asyncfunction(req,res,next){consttag=req.params.tag;letlistings=awaitdb.toPublic();listings=awaitdb.fetchByTag(tag,listings);res.render('index',{title:tag,user:req.session.user,listings:listings,scripts:JSVersionMappings,});});
Of course, if you change your mock database functions to use promises, you'll break the code that uses those database functions. You'll need to update all of it to handle the promises properly.
In a real codebase, you would start with everything asynchronous, so this wouldn't be a problem. Or, your database client would have both synchronous and asynchronous functions, so you could change them one-by-one in your code without breaking everything else.
Hope this helps.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Here's one example of where you could have asynchronous function.
Currently, you're using a mock database which you set up yourself. It works synchronously. However, most databases you would use in production projects would take a long time to respond. They would work asynchronously. They would have some API that allows you to wait for it asynchronously. Most likely, they'll return a promise.
For example, in the
helper_data.js
file, yourdb.get
function might look like this in a real codebase:In terms of the database code you have right now, an easy thing to do might be to: keep most of your code the same, but return a promise. For example:
Then, in functions that use that code, you would need to handle those promises.
For example, in routes/index.js:
Of course, if you change your mock database functions to use promises, you'll break the code that uses those database functions. You'll need to update all of it to handle the promises properly.
In a real codebase, you would start with everything asynchronous, so this wouldn't be a problem. Or, your database client would have both synchronous and asynchronous functions, so you could change them one-by-one in your code without breaking everything else.
Hope this helps.