惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Last Week in AI
Last Week in AI
D
DataBreaches.Net
腾讯CDC
Recent Announcements
Recent Announcements
有赞技术团队
有赞技术团队
A
About on SuperTechFans
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
云风的 BLOG
云风的 BLOG
罗磊的独立博客
月光博客
月光博客
MyScale Blog
MyScale Blog
U
Unit 42
Martin Fowler
Martin Fowler
Stack Overflow Blog
Stack Overflow Blog
T
Tailwind CSS Blog
Engineering at Meta
Engineering at Meta
N
Netflix TechBlog - Medium
G
Google Developers Blog
博客园 - 【当耐特】
D
Docker
I
InfoQ
雷峰网
雷峰网

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Promises: Then's Second Argument
Samuel Rouse · 2026-05-14 · via DEV Community
Cover image for Promises: Then's Second Argument

Samuel Rouse

You may already know that .then() accepts two arguments:

then(onFulfilled, onRejected)

Enter fullscreen mode Exit fullscreen mode

If either argument is omitted or not a function, it is replaced with an identity or, for rejection, a "thrower".

// Conceptually what `.then()` does with no arguments
.then((x) => x, (x) => { throw x; })

Enter fullscreen mode Exit fullscreen mode

The second argument alone is not only equivalent to .catch(), catch is just a wrapper around .then(undefined, onRejected).

But there's an interesting difference when you specify two: then's second argument is parallel. Not that is operates at the same time, but it creates a different and separate path through the chain. The two arguments are isolated from each other.

Consider this:

// Divergent paths through the code
getUserSettings()
  .then(transformSettings, loadDefaults)
  .then(showSettingsPage)
  .catch(reportError);

Enter fullscreen mode Exit fullscreen mode

Neither of these alternatives are exactly the same:

// Then first
getUserSettings()
  .then(transformSettings)
  // errors in transformSettings will trigger loadDefaults
  .catch(loadDefaults)
  .then(showSettingsPage)
  .catch(reportError);

// Catch first
getUserSettings()
  .catch(loadDefaults)
  // transformSettings will now process defaults
  .then(transformSettings)
  .then(showSettingsPage)
  .catch(reportError);

Enter fullscreen mode Exit fullscreen mode

Using both arguments of .then() allows you to create truly divergent paths purely with promises.

Awaiting Alternatives

You can replicate this with async await, but it either becomes an operation spanning multiple functions, eliminating much of the benefit of async/await, or you have to use extra state to manage it.

// async replacement for action().then(a, b)
// Pass all parts to a helper
async function thenWrap(action, a, b) {
  let data;
  try {
    data = await action();
  } catch (error) {
    return b(error);
  }
  return a(data);
}

Enter fullscreen mode Exit fullscreen mode

Keeping it within one function requires a little extra state to ensure we don't execute the ".then()" after the ".catch()".

async function example() {
  let data;
  let processed = false;
  try {
    data = await action();
  } catch (error) {
    data = b(error);
    processed = true;
  }
  if (!processed) {
    data = a(data);
  }
  // Do more
}

Enter fullscreen mode Exit fullscreen mode

That's Settled

The async solution is a bit like how one might solve the same problem with Promise.allSettled(). Because you always execute the same .then() but you receive an array of objects that communicate success or error, you can mix the declarative functional style of promises and the imperative style of aync/await.

function settledWrap(action, a, b) {
  Promise.allSettled([action()])
  .then(([result]) => {
    if (result.status === 'fulfilled') {
      return a(result.value);
    }
    return b(result.reason);
  });
}

Enter fullscreen mode Exit fullscreen mode

Conclusion

I continue to be a proponent of using promise chains over async/await particularly when it comes to structuring program flow control, as the promise chain can provide a neat abstraction away from the implementation details of the functions.

You can design your functions or program flow to never need the benefit of passing two parallel arguments to .then(), but it can be an elegant solution when you want to keep your code paths separate.