Sobes.tech
Junior — Middle

What consequences may arise if the await operator is not used in an asynchronous function?

sobes.tech AI

Answer from AI

If you do not use the await operator when calling another coroutine or asynchronous operation in an asynchronous function:

  • The asynchronous operation will not be executed immediately; instead, a coroutine object will be returned, which will not run automatically.
  • The program logic may continue execution without waiting for the asynchronous operation to complete, leading to errors or incorrect behavior.
  • The purpose of asynchrony is lost, as the task will not be suspended to wait for the result.

Example:

async def fetch_data():
    return 42

async def main():
    data = fetch_data()  # without await — data is a coroutine, not the result
    print(data)  # will print the coroutine object, not 42

import asyncio
asyncio.run(main())

The correct approach is to use await fetch_data() to obtain the result.