PY
JS

Python to JavaScript

11 lessons

Progress0%
1Variables and Constants2Functions and Closures3Lists vs Arrays4Dicts vs Objects and Map5Classes and Prototypes6Modules and Imports7Async Programming8DOM and Browser APIs9Type Hints vs TypeScript10Error Handling11Build Tools and npm
All Mirror Courses
PY
JS
Async Programming
MirrorLesson 7 of 11
Lesson 7

Async Programming

Promises, async/await, and the event loop

Introduction

In this lesson, you'll learn about async programming in JavaScript. Coming from Python, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
PY
From Python:

In Python, you're familiar with promises, async/await, and the event loop.

JS
In JavaScript:

JavaScript has its own approach to promises, async/await, and the event loop, which we'll explore step by step.

The JavaScript Way

Let's see how JavaScript handles this concept. Here's a typical example:

JS
JavaScript Example
// Promise-based fetch (built-in)
async function fetchData(url) {
  const res  = await fetch(url);
  const data = await res.json();
  return data;
}

async function main() {
  // Sequential
  const a = await fetchData("https://api.example.com/a");

  // Parallel
  const [b, c] = await Promise.all([
    fetchData("https://api.example.com/b"),
    fetchData("https://api.example.com/c"),
  ]);
}

main(); // event loop runs automatically in browser/Node

Comparing to Python

Here's how you might have written similar code in Python:

PY
Python (What you know)
import asyncio
import aiohttp

async def fetch(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as resp:
            return await resp.json()

async def main():
    # Sequential
    a = await fetch("https://api.example.com/a")

    # Parallel
    b, c = await asyncio.gather(
        fetch("https://api.example.com/b"),
        fetch("https://api.example.com/c"),
    )

asyncio.run(main())
Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JS
In JavaScript:

JavaScript's event loop runs automatically — no asyncio.run() needed

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JS
In JavaScript:

fetch() is built-in (browser + Node 18+); Python needs aiohttp

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JS
In JavaScript:

Promise.all() ≈ asyncio.gather()

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JS
In JavaScript:

Promise chaining (.then/.catch) has no Python equivalent

Step-by-Step Breakdown

1. async/await

The syntax is nearly identical. The big difference is JavaScript's event loop runs automatically.

PY
Python
async def fn():
    result = await coro()
asyncio.run(fn())
JS
JavaScript
async function fn() {
  const result = await promise();
}
fn(); // no run() needed

2. Parallel Execution

Promise.all() is JavaScript's asyncio.gather() — both run coroutines/promises concurrently.

PY
Python
a, b = await asyncio.gather(coro1(), coro2())
JS
JavaScript
const [a, b] = await Promise.all([promise1(), promise2()]);

3. Error Handling

Use try/catch with async/await in both languages.

PY
Python
try:
    data = await fetch(url)
except aiohttp.ClientError as e:
    print(e)
JS
JavaScript
try {
  const data = await fetchData(url);
} catch (err) {
  console.error(err);
}
Common Pitfall
Unhandled promise rejections crash Node.js processes. Always await inside try/catch.

Common Mistakes

When coming from Python, developers often make these mistakes:

  • JavaScript's event loop runs automatically — no asyncio.run() needed
  • fetch() is built-in (browser + Node 18+); Python needs aiohttp
  • Promise.all() ≈ asyncio.gather()
Common Pitfall
Don't assume JavaScript works exactly like Python. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • No asyncio.run() — JS event loop is always running
  • Promise.all() ≈ asyncio.gather()
  • try/catch works the same for async errors
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Python code in JavaScript to practice these concepts.
PreviousNext