concept await in category javascript

This is an excerpt from Manning's book Testing JavaScript Applications.
Listing 2.13. cart.test.js
const { db, closeConnection } = require("./dbConnection"); const { createCart } = require("./cart"); test("createCart creates a cart for a username", async () => { await db("carts").truncate(); #1 await createCart("Lucas da Costa"); const result = await db.select("username").from("carts"); #2 expect(result).toEqual([{ username: "Lucas da Costa" }]); await closeConnection(); #3 });This time, you have asynchronous functions that you need to wait for by using
await. Having to useawaitwill cause you to make the function passed to Jest’stestanasyncfunction.
Listing 2.13. cart.test.js
const { db, closeConnection } = require("./dbConnection"); const { createCart } = require("./cart"); test("createCart creates a cart for a username", async () => { await db("carts").truncate(); #1 await createCart("Lucas da Costa"); const result = await db.select("username").from("carts"); #2 expect(result).toEqual([{ username: "Lucas da Costa" }]); await closeConnection(); #3 });

This is an excerpt from Manning's book The Joy of JavaScript.
At a glance, you will notice the embellishments added to the function definition (async) and to each asynchronous call (await). This feature is available after Node 8 LTS. The intention behind this feature is that your code continues to be written as if all I/O calls blocked, when in fact they are all asynchronous and the event loop is not sacrificed waiting for data. We could have written this code using promises or (less desirably) using callbacks. In any case, it won’t look as streamlined as this. It’s important to keep in mind that this syntactic sugar is just masking the fact that it’s using promises behind the scenes. Hence, calling:
const result = await countBlocksInFile(filename) console.log(result)Prints the actual number of blocks in the file. But calling it without await you’ll need to use then to resolve the underlying pending promise and continue the flow of execution:
Figure 8.10 Using async/await follow the same chaining rules as promises
![]()
The results returned from the generator are promises, so you need to use for await...of syntax to consume it. Back in listing 8.5, we looked at a function that uses async iteration. Here’s that function once again:
async function countBlocksInFile(file) { try { await fsp.access(file, fs.constants.F_OK | fs.constants.R_OK) const dataStream = fs.createReadStream(file, { encoding: 'utf8', highWaterMark: 64 }) let previousDecodedData = '' let totalBlocks = 0 for await (const chunk of dataStream) { //#A previousDecodedData += chunk let separatorIndex while ((separatorIndex = previousDecodedData.indexOf(';')) >= 0) { const decodedData = previousDecodedData.slice(0, separatorIndex + 1) const blocks = tokenize(';', decodedData) .filter(str => str.length > 0) totalBlocks += count(blocks) previousDecodedData = previousDecodedData.slice(separatorIndex + 1) } } if (previousDecodedData.length > 0) { totalBlocks += 1 } return totalBlocks } catch (e) { console.error(`Error processing file: ${e.message}`) return 0 } }

This is an excerpt from Manning's book Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond.
The awesome thing about learning how to write asynchronous code using generators, promises, and a runner function is that you don’t have to learn anything new to make the jump to asynchronous functions. That’s because async functions are just syntactic sugar for the generators + promises approach. All you really need to learn are the new keywords. To indicate that a function is an async function, you prefix it with the async keyword. Then, instead of using the keyword yield, you use the keyword await. That’s all!
If any of the promises that you await reject, you can handle the rejection from outside async function like so: