0

I have a function called "test_sheet" that is supposed to return a value. that value will then be passed to a tester function which will tell me if I passed or failed the test.
inside my "test_sheet" I have a few async operations which are handled by promises. now, how can I return a (non-promise) value from my test_sheet function.

function test_sheet()
{
   //all my logic will go here

   new Promise(function(resolve, reject)
   {
      //simulating an async operation
      setTimeout(() => resolve(true), 1000);
   })
   .then(function(res){return res});
}

function tester()
{
   //not allowed to change this function in any way
   if(test_sheet() == true)
       console.log("pass!");
   else
       console.log("fail!");
}

tester();

Is there any better way of doing this?

  • If you're not allowed to change the `tester` function, you'll need a `test_sheet` `boolean` variable not a `function`. – Titus Aug 16 '18 at 16:51
  • 2
    May be a special case of http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call or may be not. Please, provide all relevant details and https://stackoverflow.com/help/mcve , preferably that reflect your real case and not just 'test' abstractions, this may affect how the problem can be solved. This is clearly not the code you're using, `test_sheet == true` is always false because it's a function, not a boolean. – Estus Flask Aug 16 '18 at 16:51
  • Change the function named tester and make it accept an argument, that argument will be the result of the resolved promise defined in test_sheet so you can just call tester in your resolve handler in the test_sheet function. – mpm Aug 16 '18 at 16:57
  • @estus i just tried to simplify the problem so it was easier for you to understand. actual problem is that I am supposed to submit a test for an online course. my test_sheet function is supposed to check if a string exists in a database or not (i am using JSON files and FileSystem API in node js) and return true or false. i am making an async database check and once i get the data i will return true if it exists. – madhur acharya Aug 16 '18 at 17:58
  • the file they gave me has 2 functions. an empty function called test_sheet and an already written function called tester. all my code goes into test_sheet, nothing outside. i am not allowed to change change the code i the tester function. i can only run it. – madhur acharya Aug 16 '18 at 17:59
  • If `tester` is hard-coded to call `test_sheet()` as synchronous function, that's just impossible, nothing can be done here. This is exactly what http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call problem is about. It's unclear whether there other ways to solve the problem stated in your test. Possibly you need to switch to sync FS API. – Estus Flask Aug 16 '18 at 19:55
  • I was thinking of the same thing. but that could cause performance issues. this is just test1 out of a possible 5. the others might have me add extra functionality to it (i need to pass this one to move further). I'm pretty sure this would involve creating a server. so handling multiple requests and syncFileSystem calls could cause problems. is there any other way fo doing this? (with or without promises, either way) – madhur acharya Aug 17 '18 at 06:30

3 Answers3

2

Well, technically it is possible, tester() may reamain intact:

var test_sheet=false;
function start_test()
{
   //all my logic will go here

   new Promise(function(resolve, reject)
   {
      //simulating an async operation
      setTimeout(() => resolve(true), 1000);
   })
   .then(res => {
      test_sheet=true;
      tester();
   });
}

function tester()
{
   //not allowed to change this function in any way
   test_sheet == true ? console.log("pass!") : console.log("fail!");
}

//tester();
start_test();

But the test starts with start_test() now, and test_sheet became a variable, with the sole purpose of acting as an argument - which could not be added to testing() without modifying it.
A nonworking bad design is transformed to working bad desing this way.

tevemadar
  • 12,389
  • 3
  • 21
  • 49
  • all my code should be inside the test_sheet function... I know its stupid but those are the rules. – madhur acharya Aug 16 '18 at 17:47
  • @madhuracharya `test_sheet()` being a function and the test `test_sheet == true ? ...` can not exist together. – tevemadar Aug 16 '18 at 17:54
  • I'm so sorry I meant to call the function test_sheet(). just thought using an expression might save some space. my bad I've updated my original post – madhur acharya Aug 16 '18 at 18:06
0

test_sheet() always returns a promise so try to get it resolved using async await or .then which feeds into the tester() function.

call you function this way:

test_sheet().then(function(test_sheet){
tester(test_sheet)})

for this you need to pass the boolean return value from test_sheet() to tester(test_sheet)

PJAutomator
  • 344
  • 3
  • 12
  • I am not allowed to touch the tester function or use it in any way. tester calls my function. and also all of my code should be inside the test_sheet function. so I cant use. .then() on it. – madhur acharya Aug 16 '18 at 17:40
-1

If you handle asynchronous code you have to use promise or callback and handle with async/await to change them to synchronous code

For example

function test_sheet()
{
   //all my logic will go here

   return new Promise(function(resolve, reject) {
      //simulating an async operation
      setTimeout(() => resolve(true), 2000);
   })
}

async function tester()
{
   //not allowed to change this function in any way
   await test_sheet() == true ? console.log("pass!") : console.log("fail!");
}

tester();
obba
  • 77
  • 3
  • 1
    I'm not down voting, but did you miss the part about _"not allowed to change this function in any way"_ – Ryan Wheale Aug 16 '18 at 17:10
  • If you handle asynchronous code. I think there has 3 ways async/await, promise, callback. So the question is "Is there any better way of doing this?". And I just post the better way by doing that. btw the mistake that test_sheet is a function not a boolean so it has no sense to solve by do not change that line. – obba Aug 16 '18 at 17:31
  • 1
    I don't understand your last comment, but the OP explicitly said that he cannot change the `tester()` function. Your answer changes it. If you don't fix your answer I _will_ down vote it. – Ryan Wheale Aug 16 '18 at 17:35
  • when I said not allowed to change the tester function, I meant that I am not allowed to add or change the code in the tester function. only view the result. – madhur acharya Aug 16 '18 at 17:44
  • If the question is making a bad design to be a possible. just down vote me. So sorry :( – obba Aug 16 '18 at 17:52
  • The provided solution changes the code inside the `tester()` function by adding `await`. The OP has now stated twice that this cannot be changed. If the anwer gets fixed, I will remove the down vote. – Ryan Wheale Aug 16 '18 at 18:07
  • 1
    the file they gave me has 2 functions. an empty function called test_sheet and an already written function called tester. all my code goes into test_sheet, nothing outside. I am not allowed to change the code I the tester function. I can only run it. I'm sorry if my question really confuses you, but that is all they told me. – madhur acharya Aug 16 '18 at 18:08
  • I see. Other way can you change your function to be pure synchronous what kind of async you using – obba Aug 16 '18 at 18:20