0

My question is if is it possible to achieve the following implementation in NodeJS without using any callback or asnyc/await or promises or synchrounous libraries in the caller file?

Caller File:

const isUnique = require("is-unique");
if(!isUnique({email:req.user.email})){
     //call is-unique to check if unique, if not unique return 400
     return res.status(400).json({"error":"Profile exist"})
}

Callee File:

// is-unique.js
const isUnique = (field) => {
    //call an async function countRecordsInDatabase, check the result and return a boolean
    const count = countRecordsInDatabaseAsync(field);
    return (count <= 0);
}

module.exports = isUnique;

Thanks in advance and hope I have made the question short and sweet. :D

Tobias
  • 11
  • 1
  • Possible duplicate of [Call An Asynchronous Javascript Function Synchronously](https://stackoverflow.com/questions/9121902/call-an-asynchronous-javascript-function-synchronously) – tbhaxor Mar 04 '19 at 19:05
  • Can you explain *why* you want to do it in a way that's different from the standard ways node provides? – Paul Mar 04 '19 at 19:05
  • @Paul The reason why is because I am trying to let the caller function(in Caller File), to not have any codes on implementing the callback function. I code mostly in PHP and I wanted to check if it is possible to achieve the following implementation in NodeJS without using any callback or asnyc/await or promises or synchrounous libraries – Tobias Mar 05 '19 at 01:11
  • That's just restating what you're trying to do, not why you want to do it that way. – Paul Mar 05 '19 at 02:49
  • Why I would want to do it is because I am trying to find out if there are any other ways I can do it other than using callback or asnyc/await or promises or synchrounous libraries. Its to explore the possibilities of how it can be done. – Tobias Mar 05 '19 at 16:35

1 Answers1

0

You want to call an async function, in a sync function and not have the sync function be async. It seems like that is what you are asking.

There are libraries that suggest they can do this, however it doesn't sound like a great idea.

What are you trying to achieve?

I'll keep it real with you. If most people aren't doing things this way, which they aren't it's prolly not a good idea to do it differently.

Lpc_dark
  • 2,834
  • 7
  • 32
  • 49
  • Thanks for the quick reply! What im trying to achieve is to let the caller function to have as minimal codes as possible. Meaning to say, all the caller needs to do is to call a function to know with some inputs. Example: ``` if(isUnique(param1,param2){ //dosth } else{ //doanotherthing } ``` instead of having ``` isUnique(param1,param2, (result)=>{ //callback todos here }) ``` – Tobias Mar 04 '19 at 18:10