0

i have the following function that should return a result object

function checkRegisteredStatus(addr)
{
  let result ={status:"",registrationType:""};
  instance.methods.isRegistered(session.id,addr).call().then((receipt)=>
  {
    let {status,registrationType}= receipt;  
    result["status"] = status;
    result["registrationType"]= registrationType;
  });
  return result;
}

i want to use this function like this:

let result = checkRegisteredStatus(addr)
  • i have three problems :

1- when i used async/await pattern on isRegistered() method like this:

    let result = await instance.methods.isRegistered(session.id,addr).call();
    return result;

result object would be a promise with unfulfilled status, which is wired since the purpose of await is to return the result of the resolve callback, and this is the first time that happens to me, i did async/await before and it always returns the final result not a promise. why is this happening?

2- due to the first problem i had to re-write my function the old way which is using.then() method (as i pasted above) and it works, however i dont understand how come the checkRegisteredStatus function should finish execution and is popped off from the call stack then how is it being able to modify the result object?

Example:
let result = checkRegisteredStatus(addr)
console.log(result)

the output would be:

> {status:"",registration:""}

when i click the expand sign > it outputs the following:

 > {status:"",registration:""}
      registrationType: "NotRegistered"
      status: false

as far as i understand the value was caught be console.log() when the result object still had empty properties that's why i could expand the object and see its props have different values,i think JS is adding elements from inside that promise that registered before, but the function result obj should be cleaned from the stack (since it returns immediately), how is it adding values to it even though the function is popped from the call-stack?

3- how should i rewrite my function such that it blocks execution until it returns the final result not a promise and not adding up values later.

ezio
  • 359
  • 2
  • 13
  • It should work with your async/await solution. Did you add `await` keyword when calling function(after you rewrote to async/await syntax), something like this `let result = await checkRegisteredStatus(addr)`? – Milos Pavlovic Jun 27 '22 at 11:30
  • *"such that it blocks execution until it returns the final result not a promise"* — You can't, end of story. The value *will* be available *sometime later*, so you need to work with callbacks or promises. – deceze Jun 27 '22 at 11:33
  • @deceze such a horrible end of story, any resources why? – ezio Jun 27 '22 at 11:36
  • @MilosPavlovic yeah i did – ezio Jun 27 '22 at 11:36
  • Because it's the very fundamental way of how async code works. Read the duplicate linked above for details and links to even more details. – deceze Jun 27 '22 at 11:37
  • I would really like to see this piece of code on codesandbox in order to inspect on my own. To me it looks like an ordinary async execution in javascript. Everything that can be done with .then can be also done with async/await, it is just a syntactic sugar for cleaner code, nothing else. At least provide whole async/await implementation so I can get better picture. If `isRegistered` returns promise, and it does. it really should work with async/await. – Milos Pavlovic Jun 27 '22 at 11:48
  • @Milos There is no way that `let result = checkRegisteredStatus(addr)` can directly return the desired data, when internally it makes an asynchronous call. No need to reproduce. – deceze Jun 27 '22 at 11:52
  • @deceze That is clear, and that is why I asked him if he rewrote that part to `let result = await checkRegisteredStatus(addr)`, because only with await it will pause the execution and wait for promise to resolve. – Milos Pavlovic Jun 27 '22 at 11:56
  • @Milos Yes, but OP wants to "rewrite my function such that it blocks execution until it returns the final result not a promise and not adding up values later". So I guess `await checkRegisteredStatus(addr)` is the one thing they don't want and perhaps didn't even try and it's simply a no-go. – deceze Jun 27 '22 at 11:57
  • @deceze Okay, I got the point, if with "blocks execution" OP literally means to synchronously block call stack for some amount of the time, then yes, I agree it is not possible. But I was confused because OP replied to me that he/she added `await` keyword in front of `checkRegisteredStatus` and still not working, which really does not make sense to me. – Milos Pavlovic Jun 27 '22 at 12:02
  • @Milos Then they probably left the innards of the function with the callback instead of properly `await`ing internally as well. Just my guess… – deceze Jun 27 '22 at 12:12
  • @MilosPavlovic sorry i thought you asked whether i added it in isRegistered function not in checkRegistered(), now it works, i assume whenever the function reaches the await keyword it returns immediately to the caller a promise and i thought that it assigned a promise to the result object and returned it which is why i didn't bother to add the await keyword from outside, however i did not add a resolve(result) inside checkRegistered, so how is await able to get the result like this; let res = await checkRegistered(addr), as far as i understand await should take the result of resolve() right! – ezio Jun 27 '22 at 12:46
  • 1
    No need to mix promise callbacks with async/await. I suggest you to rewrite whole `checkRegistered` in async/await style, it is far more natural, especially if you consume that function with `await` keyword. Solution #1 that you provided in OP is completely fine, you were properly using async/await with simple and readable two lines of code. Only thing that you were missing is to await when calling `checkRegistered`. – Milos Pavlovic Jun 27 '22 at 13:00
  • @MilosPavlovic yeah, now it works great thanks to you man, i guess javascript is double edged sword, it can be for beginners but it can also make you fall in mistakes that only advanced users could solve. – ezio Jun 27 '22 at 13:09

0 Answers0