Welcome back! You've mastered sequential workflows with prompt chaining and conditional workflows with intelligent routing. Now it's time to unlock dramatic performance improvements by learning parallel processing — executing multiple independent OpenAI API calls simultaneously instead of waiting for each one to complete.
In this lesson, you'll discover how to transform workflows that take minutes into operations that complete in seconds. You'll learn the difference between synchronous and asynchronous programming, master TypeScript's native Promise-based concurrency, understand the critical differences between Promise.all() and Promise.allSettled(), and build a production-ready system that asks multiple questions to GPT-5 at the same time while handling failures gracefully.
Before we dive into the technical details, let's clarify terms that often confuse beginners: synchronous and asynchronous.
Outside of programming, "synchronous" means "happening at the same time" — think synchronized swimming or clocks ticking together. In programming, though, "synchronous" means the opposite: operations are coordinated in sequence, not in parallel.
When we say "synchronous API calls," we mean calls that happen one after another, waiting for each to complete before starting the next. "Asynchronous" API calls, on the other hand, can be launched together and run concurrently — they don't wait for each other to finish.
Here's what makes this possible: Node.js (the runtime for TypeScript) uses an event loop and non-blocking I/O. When you make an asynchronous API call to OpenAI, your code doesn't actually freeze and wait for the response. Instead, Node.js registers your request and immediately continues executing other code. When OpenAI's response arrives, the event loop triggers a callback to process the result.
This architecture is what enables true concurrency in a single-threaded environment — while one API call waits for GPT-5's response over the network, Node.js can initiate and manage other API calls without blocking. Think of it like a restaurant server who takes multiple tables' orders without waiting at each table for the kitchen to finish cooking.
This might seem backward at first, but once you understand this distinction, the terms "synchronous" (sequential) and "asynchronous" (concurrent, non-blocking) will make much more sense throughout this lesson.
Let's understand the high-level pattern we'll be implementing. This workflow has two distinct phases that work together to provide both speed and comprehensive results:
Phase 1: Parallel Research Gathering
- Launch multiple independent
OpenAIAPI calls simultaneously. - Each call researches a different aspect of your topic (attractions, transportation, culture).
- All questions run concurrently, completing in roughly the time of the slowest individual request.
- Results are collected and preserved, even if some requests fail.
Phase 2: Sequential Result Synthesis
- Combine all successful parallel research into a single comprehensive dataset.
- Send the aggregated information to
GPT-5with instructions for synthesis. - Generate a unified, actionable final result (like a complete travel guide).
- This sequential step ensures all information is properly integrated.
This two-phase approach maximizes both efficiency and quality: you get the speed benefits of parallel processing for data gathering while maintaining coherent analysis through sequential aggregation. It's particularly powerful for research tasks, analysis workflows, and any scenario where you need to quickly gather diverse information and synthesize it into actionable insights.
TypeScript (and JavaScript) has built-in support for asynchronous operations through Promises and the async/await syntax. Unlike some languages that require separate libraries for concurrent execution, TypeScript's asynchronous capabilities are native to the language runtime.
A Promise represents a value that may not be available yet but will be resolved in the future. When you call an asynchronous function, it immediately returns a Promise, allowing your program to continue executing other code while waiting for the operation to complete.
The async keyword transforms a regular function into one that returns a Promise, while await pauses execution of that function until the Promise resolves. Importantly, when a function is paused at an await, the JavaScript runtime can execute other code — this is what enables concurrent operations.
Unlike some languages that require special setup to run asynchronous code, TypeScript's async functions can be called directly. You simply invoke the async function and handle its Promise:
The real power of async programming comes from running multiple operations concurrently. TypeScript provides two main approaches for this: Promise.all() and Promise.allSettled(). Understanding the difference between them is crucial for building robust workflows.
Promise.all() starts multiple Promises simultaneously and waits for all of them to complete, returning results in the original order:
This looks elegant, but Promise.all() has a critical limitation: fail-fast behavior. If any single Promise rejects (for example, if one API call fails due to network issues, rate limits, or invalid requests), Promise.all() immediately rejects and discards all other results — even if they completed successfully.
This behavior is problematic for real-world GPT-5 workflows. API calls can fail for many reasons:
- Network connectivity issues.
- Rate limiting from
OpenAI. - Invalid prompts or parameters.
- Temporary service disruptions.
- Token limit exceeded for one particular request.
If you're researching ten topics and one fails, you still want the other nine answers rather than getting nothing at all.
Promise.allSettled() solves the fail-fast problem by waiting for all Promises to complete, regardless of whether they succeed or fail. It returns an array of result objects, each indicating whether its Promise was fulfilled or rejected:
The PromiseSettledResult type has two possible shapes:
{ status: "fulfilled", value: T }— contains the successful result.{ status: "rejected", reason: any }— contains the error that caused rejection.
This structure allows you to handle successes and failures independently, ensuring no valuable results are lost even when some requests fail.
Use Promise.all() when:
- All operations must succeed for the workflow to be valid.
- A single failure should abort the entire process.
- You're in a controlled environment with high reliability.
- You want simpler code for tutorial examples.
Use Promise.allSettled() when:
- Partial results are still valuable (most research/analysis workflows).
- You need to handle individual failures gracefully.
- You're building production systems where resilience matters.
- You want to log which specific requests failed while using successful ones.
For GPT-5 workflows, Promise.allSettled() is almost always the better choice because it ensures you get maximum value from your parallel execution even when some requests encounter issues.
Now let's build a complete parallel workflow that demonstrates both speed and resilience. We'll create a travel planning system that researches multiple independent topics simultaneously and synthesizes them into a comprehensive guide.
First, let's create an async function that handles individual research questions:
Key design decisions:
- Return type
Promise<[string, string]>: Returns a tuple so we can easily match questions to answers. - Console logging: Helps visualize when each question starts and completes during parallel execution.
- Minimal reasoning: Since these are independent research questions, we don't need deep reasoning yet.
Next, we'll define the questions that will execute in parallel. These should be independent — none should depend on answers from the others:
These questions cover different aspects of travel planning (attractions, transportation, culture) and can be researched simultaneously without any dependencies.
Now we'll launch all questions in parallel using Promise.allSettled() to ensure we get all successful results even if some fail:
Important TypeScript features in this code:
- Type guard
result is PromiseFulfilledResult<[string, string]>: Tells TypeScript that filtered results contain thevalueproperty with our tuple type. - Destructuring in
.filter()and.map(): Cleanly separates successful results from failures. - Status counts: Provides visibility into how many requests succeeded vs. failed.
This approach ensures that even if one or two questions fail due to network issues or API problems, you still get all the successful research results and can generate a useful (though perhaps incomplete) travel guide.
With all parallel research complete, we need to synthesize the results into a comprehensive guide. This sequential step ensures all information is properly integrated:
Key design considerations:
- Edge case handling: Returns a clear message if all research failed.
- Structured formatting: Clearly labels questions and answers for
GPT-5. - Flexible synthesis instructions: Tells
GPT-5to work with available data, even if incomplete. - Minimal reasoning: We're synthesizing already-researched information, not doing new analysis.
Finally, let's connect the parallel research phase to the sequential synthesis phase:
When you run this workflow, you'll observe three distinct phases that demonstrate the power of parallel processing:
Phase 1: Instant Launch (happens immediately)
All three "🔄 Asking" messages appear immediately because the API calls fire off simultaneously, not sequentially.
Phase 2: Concurrent Completion (may arrive in any order)
The "✅ Answered" messages arrive as GPT-5 finishes each response — often in a different order than they were asked. This proves your requests are truly running in parallel, not waiting for each other.
Phase 3: Intelligent Synthesis (sequential aggregation)
All the concurrent research gets woven together into a comprehensive travel guide that combines the speed benefits of parallel processing with thoughtful analysis.
Here's what the full workflow output looks like:
This parallel workflow pattern provides dramatic performance improvements while maintaining result quality:
Speed Comparison:
- Sequential execution: 3 questions × 3-5 seconds each = 9-15 seconds.
- Parallel execution: ~3-5 seconds total (time of slowest request).
- Performance gain: 3x faster or more.
Resilience Benefits:
- Partial results are preserved when individual requests fail.
- Detailed error logging helps debug specific failures.
- The travel guide can still be generated with available data.
- Production systems remain operational even with intermittent API issues.
When to Use This Pattern:
- Research tasks requiring multiple independent investigations.
- Data gathering that can be parallelized (market research, competitive analysis).
- Scenarios where partial results provide value.
- High-latency operations that benefit from concurrency.
- Production systems requiring resilience to individual failures.
When to Avoid This Pattern:
- Questions that depend on previous answers (use sequential chaining instead).
- Operations that must all succeed or all fail atomically.
- Rate-limited scenarios where parallel requests cause throttling.
- Simple single-question workflows where concurrency adds complexity without benefit.
You've mastered production-ready parallel processing patterns that transform slow sequential workflows into lightning-fast concurrent operations. The key insights you've learned:
- Asynchronous fundamentals: Understanding Node.js's event loop and non-blocking I/O.
- Promise.all vs Promise.allSettled: Knowing when to use fail-fast vs resilient execution.
- Two-phase workflow: Parallel data gathering + sequential synthesis.
- Error handling: Gracefully managing partial failures in production systems.
- TypeScript type safety: Using type guards and proper Promise typing.
The combination of Promise.allSettled() for parallel research gathering and sequential result synthesis provides both speed and quality, making it ideal for complex analysis tasks like travel planning, market research, or technical evaluations.
In the upcoming exercises, you'll apply these patterns to real-world scenarios, handle edge cases, and learn to optimize concurrent GPT-5 workflows for various use cases. Remember: use parallel processing with Promise.allSettled() for independent research tasks where partial results have value, then aggregate results sequentially for comprehensive final analysis.
