liveBook · Manning

Async/await

As explained in The Art of Unit Testing 3e

Async/await is JavaScript syntax for expressing promise-based asynchronous control flow in a sequential-looking form.

Async/await expresses promise-based asynchronous work in a sequential-looking form. It makes control flow easier to read without making the underlying operation synchronous. section 6.1 section 6.1.3

Compared with callback-based code, async/await offers practical testing advantages:

  • Use await directly in an async test instead of combining callbacks, done(), and then(). section 6.1.3
  • Treat the test more like Arrange-Act-Assert and inspect the returned value at one clear entry and exit point. section 6.1.3
  • Remember that await simplifies the surface syntax; the test remains asynchronous and is still an integration test. section 6.1.3

Awaiting a website check

An async test can await a website-checking function, then assert its result: const result = await samples.isWebsiteAliveWithAsyncAwait(); expect(result.success).toBe(true); expect(result.status).toBe("ok"); section 6.1.3

Figure 6.1 IsWebsiteAlive() callback vs. the async/await version
06-01

Compare callback and async/await entry and exit points in the website-checking example. section 6.1

How The Art of Unit Testing 3e covers it

How deep it goes
Explains promise-based syntax, contrasts callbacks, and demonstrates testing while retaining the underlying asynchronous behavior.
Examples
Demonstrates a node-fetch website check, missing-text and connectivity failures, and an awaited integration test.
What it assumes
Assumes knowledge of JavaScript promises while introducing the callback-to-async/await comparison through concrete examples.
What you can do after
Apply it via async functions and await-based tests that inspect asynchronous return values clearly.
How it connects
Connects promises, callbacks, asynchronous code, integration testing, and the Arrange-Act-Assert pattern.

Also explained in

Compare how 17 books explain async/await →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Art of Unit Testing 3e. The section links point to the parts of the book it draws on.