From reading about async / await, I understood that this construction should simplify the code, save developers from unnecessary callbacks. I ran the following example:

var request = require('request'); var async = require('asyncawait/async'); var await = require('asyncawait/await'); function getQuote() { var quote; return new Promise(function(resolve, reject) { request('http://ron-swanson-quotes.herokuapp.com/v2/quotes', function(error, response, body) { quote = body; resolve(quote); }); }); } var main = async (()=>{ var quote = await(getQuote()); console.log(quote); }); main(); console.log('Ron once said,'); 

In the console I see the following:

 Ron once said, ["Great job, everyone..."] 

This suggests that main() works after console.log('Ron once said,') .

But what is the point then if one-hell needs to pass a callback to main() to first get the result of the query, and then do something with it?

How to ensure that in this example, first worked main() and then console.log() ?

  • for example add await when calling main - Grundy
  • Well, you do not do await for main () - Vladimir Gamalyan
  • I'm not quite sure, but this is some kind of wrong async / await crutch, I think it's better to wait for normal browser support - andreymal
  • 2
    @andreymal why wrong? And what have the browsers? - Qwertiy
  • @Qwertiy because instead of normal async / await, I see here a polyfill crutch on these most miserable callbacks (async / await here are functions, not operators). Okay, browsers have nothing to do with it, the node is not mentioned in the question, but I didn’t look at the tags) But this doesn’t change anything - there is still no native support for async / await in the node. - andreymal

2 answers 2

async determines that the function is asynchronous and allows the use of the await operator inside it, which pauses the execution of the function, while the result is received. I draw your attention to the fact that the await construction can be used not everywhere, but only inside asynchronous functions.

Before defining async/await similar behavior was achieved using generators bundled with libraries like co . I have already described this approach in detail in one of my answers .


Now a few words about your specific example. The main goal, as far as I can see, in the pseudo-synchronous execution of the bundle:

 main(); console.log('Ron once said,'); 

It would seem that the main function is defined as asynchronous and this block must be executed sequentially. However, you miss one very important point: the definition of an asynchronous function does not make its call pseudo-synchronous automatically . You need to explicitly wait for the result of the execution of main .

To do this, simply wrap all your code in a IIFE expression using an asynchronous function and wait for the result of executing main :

 let getData = () => { return new Promise(function(resolve, reject) { setTimeout(() => { resolve('Great job, everyone...'); }, 500); }); }; (async () => { let main = async ()=> { console.log(await getData()); }; await main(); console.log('Ron once said,'); })(); 

This example will output:

Great job, everyone ...
Ron once said,

And here is a working example on JSFiddle.

Comment:

I wrote the code above in pure JS, without using the asyncawait asyncawait . If you want to use this library, then instead of the async / await keywords, you need to explicitly call the functions of this library.

Alternatively, I would suggest using Babel , which turns the ES 6/7 code into something that can already be run in most browsers today.

     main(); 

    If you have to wait, you should indicate this:

     await (main()); 
    • So, I get an await main(); SyntaxError: Unexpected identifier error await main(); SyntaxError: Unexpected identifier await main(); SyntaxError: Unexpected identifier . for this you need to somehow run the node in compile mode through babel? - sanu0074
    • @ sanu0074, if you look at await in your case, it looks like a function, so there should be something like await( main()); - Grundy
    • @ sanu0074, added brackets. - Qwertiy
    • @Qwertiy, this also causes an error: Error: await functions, yield functions, and value-returning suspendable functions may only be called from inside a suspendable function. - sanu0074
    • one
      @Qwertiy, the await keyword can only be used in a function marked as async . - Dmitriy Simushev