GO
TS

Go to TypeScript

10 lessons

Progress0%
1Variables & Types2Functions3Structs & Classes4Interfaces — Both Use Structural Typing5Error Handling6Goroutines vs Async/Await7Slices & Maps → Arrays & Objects8Packages & Modules9Testing10Generics
All Mirror Courses
GO
TS
Goroutines vs Async/Await
MirrorLesson 6 of 10
Lesson 6

Goroutines vs Async/Await

Concurrent and asynchronous programming

Introduction

In this lesson, you'll learn about goroutines vs async/await in TypeScript. Coming from Go, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
GO
From Go:

In Go, you're familiar with concurrent and asynchronous programming.

TS
In TypeScript:

TypeScript has its own approach to concurrent and asynchronous programming, which we'll explore step by step.

The TypeScript Way

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

TS
TypeScript Example
// Promise - async operation
async function fetchData(): Promise<string> {
  const response = await fetch("/api/data");
  return response.text();
}

// Concurrent with Promise.all
const [user, posts] = await Promise.all([
  fetchUser(42),
  fetchPosts(42),
]);

// Sequential async
async function processAll(ids: number[]): Promise<void> {
  for (const id of ids) {
    await process(id);
  }
}

// Parallel with Promise.allSettled
const results = await Promise.allSettled(
  ids.map(id => process(id))
);

Comparing to Go

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

GO
Go (What you know)
// Goroutine - lightweight thread
go func() {
    result := fetchData()
    fmt.Println(result)
}()

// Channel for communication
ch := make(chan string)
go func() {
    ch <- "data ready"
}()
msg := <-ch

// WaitGroup for multiple goroutines
var wg sync.WaitGroup
for i := 0; i < 5; i++ {
    wg.Add(1)
    go func(id int) {
        defer wg.Done()
        process(id)
    }(i)
}
wg.Wait()
Mirror Card
GO
From Go:

You may be used to different syntax or behavior.

TS
In TypeScript:

Go goroutines are OS-scheduled; TypeScript async/await is event-loop based

Mirror Card
GO
From Go:

You may be used to different syntax or behavior.

TS
In TypeScript:

Go uses channels for communication; TypeScript uses Promises and callbacks

Mirror Card
GO
From Go:

You may be used to different syntax or behavior.

TS
In TypeScript:

Go can truly run code in parallel; TypeScript/JS is single-threaded (Node.js workers exist)

Mirror Card
GO
From Go:

You may be used to different syntax or behavior.

TS
In TypeScript:

Go's WaitGroup → Promise.all for waiting on multiple concurrent tasks

Step-by-Step Breakdown

1. Starting Concurrent Work

Go launches goroutines with the go keyword; TypeScript marks functions async and uses await to pause execution.

GO
Go
go processData()
TS
TypeScript
await processData(); // or: processData() // fire-and-forget

2. Waiting for Multiple Tasks

Go's WaitGroup or channel patterns correspond to Promise.all in TypeScript for running multiple async tasks concurrently.

GO
Go
results := make([]string, len(ids))
var wg sync.WaitGroup
for i, id := range ids { wg.Add(1); go func(i, id int) { defer wg.Done(); results[i] = fetch(id) }(i, id) }
wg.Wait()
TS
TypeScript
const results = await Promise.all(ids.map(id => fetch(id)));

3. Error Handling in Async

In Go, goroutine errors must be passed back via channels. In TypeScript, async function errors become rejected Promises caught with try/catch.

Rule of Thumb
Use Promise.allSettled when you want results even if some fail; Promise.all fails fast on first rejection.

Common Mistakes

When coming from Go, developers often make these mistakes:

  • Go goroutines are OS-scheduled; TypeScript async/await is event-loop based
  • Go uses channels for communication; TypeScript uses Promises and callbacks
  • Go can truly run code in parallel; TypeScript/JS is single-threaded (Node.js workers exist)
Common Pitfall
Don't assume TypeScript works exactly like Go. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • Go goroutines → TypeScript async functions
  • Go channels → TypeScript Promises
  • WaitGroup → Promise.all
  • Go is truly parallel; TypeScript is single-threaded event loop
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Go code in TypeScript to practice these concepts.
PreviousNext