Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to return a value with async/await instead of a Promise ? As a synchronous function do

Firstly I am familiar with the concept of asynchronous/synchronous function. There is also a lot of questions related to mine. But I can't find my answer anywhere.

So the question is: Is there a way to return a value instead of a Promise using async/await ? As a synchronous function do.

For example:

async doStuff(param) {
  return await new Promise((resolve, reject) => {
    setTimeout(() => {
      console.log('doStuff after a while.');
      resolve('mystuffisdone'+param);
    }, 2000);
  });
}

console.log(doStuff('1'));

The only way to get the value of this function is by using the .then function.

doStuff('1').then(response => {
  console.log(response); // output: mystuffisdone1
  doOtherStuffWithMyResponse(response);
  // ...
});

Now, what I want is:

const one = doStuff('1');
console.log(one) // mystuffisdone1
const two = doStuff('2');
console.log(two) // mystuffisdone2

To explain myself, I have an asynchronous library full of callbacks. I can turn this asynchronous behavior to a synchronous behavior by using Promises and async/await to faking a synchronous behavior. But there is still a problem, it is still asynchronous in the end; outside of the scope of the async function.

doStuff('1').then((r) => {console.log(r)};
console.log('Hello wolrd');

It will result in: Hello world then mystuffisdone1. This is the expected behavior when using async/await functions. But that's not what I want.

Now my question would be: Is there a way to do the same thing as await do without the keyword async ? To make the code being synchronous ? And if not possible, why ?

Edit:

Thank you for all you answers, I think my question is not obsvious for all. To clear up what I think here is my comment to @Nikita Isaev answer.

"I understand why all I/O operations are asynchronously done; or done in parallel. But my question is more about the fact that why the engine doesn't block the caller of the sync function in an asynchronous manner ? I mean const a = doStuff(...) is a Promise. We need to call .then to get the result of this function. But why JavaScript or Node engine does not block the caller (just the block where the call is made). If this is possible, we could do const a = doStuff(...), wait and get the result in a without blocking the main thread. As async/await does, why there is no place for sync/wait ?"

Hope this is more clear now, feel free to comment or ask anything :)

Edit 2:

All precisions of the why of the answer are in the comments of the accepted answer.

like image 880
Folkvir Avatar asked Jan 01 '18 01:01

Folkvir


People also ask

Can I use async await instead of promises?

Async/Await is used to work with promises in asynchronous functions. It is basically syntactic sugar for promises. It is just a wrapper to restyle code and make promises easier to read and use. It makes asynchronous code look more like synchronous/procedural code, which is easier to understand.

Does await return a promise or a value?

Inside an async function, you can use the await keyword before a call to a function that returns a promise. This makes the code wait at that point until the promise is settled, at which point the fulfilled value of the promise is treated as a return value, or the rejected value is thrown.

Does an async function need to return a promise?

Async functions always return a promise. If the return value of an async function is not explicitly a promise, it will be implicitly wrapped in a promise.

Does async await make it synchronous?

Async/await helps you write synchronous-looking JavaScript code that works asynchronously. Await is in an async function to ensure that all promises that are returned in the function are synchronized. With async/await, there's no use of callbacks.


2 Answers

There are some hacky ways to do what is desired, but that would be an anti-pattern. I’ll try to explain. Callbacks is one of the core concepts in javascript. When your code launches, you may set up event listeners, timers, etc. You just tell the engine to schedule some tasks: “when A happens, do B”. This is what asynchrony is. But callbacks are ugly and difficult to debug, that’s why promises and async-await were introduced. It is important to understand that this is just a syntax sugar, your code still is asynchronous when using async-await. As there are no threads in javascript, waiting for some events to fire or some complicated operations to finish in a synchronous way would block your entire application. The UI or the server would just stop responding to any other user interactions and would keep waiting for a single event to fire.

Real world cases:

Example 1.

Let’s say we have a web UI. We have a button that downloads the latest information from the server on click. Imagine we do it synchronously. What happens?

myButton.onclick = function () {
  const data = loadSomeDataSync(); // 0
  useDataSomehow(data);
}

Everything’s synchronous, the code is flat and we are happy. But the user is not.

A javascript process can only ever execute a single line of code in a particular moment. User will not be able to click other buttons, see any animations etc, the app is stuck waiting for loadSomeDataSync() to finish. Even if this lasts 3 seconds, it’s a terrible user experience, you can neither cancel nor see the progress nor do something else.

Example 2.

We have a node.js http server which has over 1 million users. For each user, we need to execute a heavy operation that lasts 5 seconds and return the result. We can do it in a synchronous or asynchronous manner. What happens if we do it in async?

  1. User 1 connects
  2. We start execution of heavy operation for user 1
  3. User 2 connects
  4. We return data for user 1
  5. We start execution of heavy operation for user 2

I.e we do everything in parallel and asap. Now imagine we do the heavy operation in a sync manner.

  1. User 1 connects
  2. We start execution of heavy operation for user 1, everyone else is waiting for it to accomplish
  3. We return data for user 1
  4. User 2 connects

Now imagine the heavy operation takes 5 seconds to accomplish, and our server is under high load, it has over 1 million users. The last one will have to wait for nearly 5 million seconds, which is definitely not ok.

That’s why:

  1. In browser and server API, most of the i/o operations are asynchronous
  2. Developers strive to make all heavy calculation asynchronous, even React renders in an asynchronous manner.
like image 149
Kit Isaev Avatar answered Sep 23 '22 06:09

Kit Isaev


No, going from promise to async/await will not get you from async code to sync code. Why? Because both are just different wrapping for the same thing. Async function returns immediately just like a promise does.

You would need to prevent the Event Loop from going to next call. Simple while(!isMyPromiseResolved){} will not work either because it will also block callback from promises so the isMyPromiseResolved flag will never be set.

BUT... There are ways to achieve what you have described without async/await. For example:

  • OPTION 1: using deasync approach. Example:
function runSync(value) {

    let isDone = false;
    let result = null;

    runAsync(value)
    .then(res => {
        result = res;
        isDone = true;
    })
    .catch(err => {
        result = err;
        isDone = true;
    })

    //magic happens here
    require('deasync').loopWhile(function(){return !isDone;});

    return result;
}

runAsync = (value) => {

    return new Promise((resolve, reject) => {

        setTimeout(() => {
            // if passed value is 1 then it is a success
            if(value == 1){
                resolve('**success**');
            }else if (value == 2){
                reject('**error**');
            }
        }, 1000);

    });

}

console.log('runSync(2): ', runSync(2));
console.log('runSync(1): ', runSync(1));

OR

  • OPTION 2: calling execFileSync('node yourScript.js') Example:
const {execFileSync} = require('child_process');
execFileSync('node',['yourScript.js']);

Both approaches will block the user thread so they should be used only for automation scripts or similar purposes.

like image 40
Wesol Avatar answered Sep 20 '22 06:09

Wesol