JavaScript - Async Await

2 min read

Async Await

  • async before a function means the function always returns a promise.
  • await works inside async functions. Waits until that promise settles and returns its result before continuing.
  • await doesn’t work in the top-level code. It needs to be contained within an async function for the code that it’s waiting for.

Example Test in Jest

1it("returns London temperature as a number", async () => {
2 const londonWeather = await weather.londonWeatherForOneDay();
3 expect(typeof londonWeather).toEqual("number");
Previous post:
Node - Fetch Is Not Defined
Next post:
Babel - RegeneratorRuntime Is Not Defined