# Run Promises in sequence

Start each task after the previous one finishes and collect the results in order.

Canonical: https://1loc.dev/misc/run-promises-in-sequence/
Version: 1.0.0
Runtime: JavaScript · Node.js 22 / modern browsers
Verification: Usage lines executed in Node.js 22 and headless Chromium; no external services
Updated: 2026-10-28

## Implementation

```javascript
async function runInSequence(tasks) {
  const results = [];
  for (const task of tasks) results.push(await task());
  return results;
}
```

## Usage

```javascript
const log = [];
const step = (name, ms) => () => new Promise(resolve => setTimeout(() => { log.push(name); resolve(name); }, ms));
await runInSequence([step('slow', 30), step('fast', 10)]); // → ['slow', 'fast']
log; // → ['slow', 'fast']
// Promises, unlike tasks, are already running:
log.length = 0;
await Promise.all([step('slow', 30)(), step('fast', 10)()]); // → ['slow', 'fast']
log; // → ['fast', 'slow']
```

Lines marked → were executed in Node.js 22 and in headless Chromium, and each result was compared with the value shown. Lines marked e.g. are illustrative.

## Limits

- Pass functions that start the work, not promises: a promise is already running once it exists. The original took promises, so it ordered the results but not the work, as the last lines show.
- Use it when each step must finish before the next starts, such as sending one batch at a time under a rate limit. On a 429, wait for Retry-After (see Read a Retry-After header) before the next task.
- The first rejection stops the sequence; catch inside each task to keep going.

The function does not validate its arguments. Add checks where it receives untrusted input.

## Source and license

Original: https://github.com/phuocng/1loc/blob/14b481cf60dff1620ab1bb99a43b1f68068cdd9b/contents/run-promises-in-sequence.mdx
Author: Nguyen Huu Phuoc and original contributors
Contributors: See original repository history
Source commit: 14b481cf60dff1620ab1bb99a43b1f68068cdd9b
Copyright (c) 2020 phuoc-ng
License: MIT — https://1loc.dev/licenses/
Maintainer: Oveyon
Modifications: Takes functions that start each task; the original took promises, which were already running concurrently. Documented as a reference with executed usage lines. Original MDX retained as inert source evidence.
