1

There is some library I use and it's function returns a promise.

function Something()
{
    return new Promise((r,e) =>
    {
        r(true);
    });
}

And I have my code:

//async function MyFunction() NO!
//function MyFunction(callback) NO!
function MyFunction() // -> BOOLEAN | void | undefined
{
    return Something();
    //return Something().then(/*actualy return*/).catch(/*return false or undefined*/);
    //Something().Wait().ReturnResult();
}

console.log(MyFunction());

How do I return boolean value? Without making my function a promise.

Man Tachi
  • 11
  • 2
  • _"How do I return boolean value?"_... you cannot. Once you start working with asynchronous code, there's no way to make it synchronous. You'd need something like `MyFunction().then(console.log)` to log the value resolved by the promise – Phil Apr 21 '22 at 05:50
  • There must be a way to make a WAIT() – Man Tachi Apr 21 '22 at 05:54
  • 1
    _"There must be a way to make a WAIT()"_ - `myFunction().then(result => { ... })` is a way to _wait_. – Yousaf Apr 21 '22 at 06:25
  • `async`/`await` – Bravo Apr 21 '22 at 06:41
  • 1
    @ManTachi `put a async where not required` - yes, that would be dumb ... just don't use the library – Bravo Apr 21 '22 at 06:47
  • @Yousaf that does not wait, that just redirects to another function – Man Tachi Apr 21 '22 at 16:45
  • @Bravo I don't see any logic in all this promise,async,await if there is no possibility to WAIT. It makes no any sense, if it is a single thread what was the actual point of making this functionalities? – Man Tachi Apr 21 '22 at 16:47
  • how is await not wait – Bravo Apr 21 '22 at 17:20
  • It does not block and WAIT for the result. It creates another Promise since where you want to await MUST be ASYNC function. It is simply a callback loop. – Man Tachi Apr 21 '22 at 19:04
  • @ManTachi Reading about [asynchronous javascript](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous) might help. – Yousaf Apr 22 '22 at 05:15

0 Answers0