Skip to content

Commit 27839dc

Browse files
committed
Add FAQ about getting results in order
Closes #167
1 parent 6b8ff60 commit 27839dc

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

readme.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,35 @@ controller.abort(); // Cancels if still queued; running tasks must handle `signa
579579

580580
Direct removal methods are not provided as they would leak internals and risk dangling promises.
581581

582+
#### How do I get results in the order they were added?
583+
584+
This package executes tasks in priority order, but doesn't guarantee completion order. If you need results in the order they were added, use `Promise.all()`, which maintains the order of the input array:
585+
586+
```js
587+
import PQueue from 'p-queue';
588+
589+
const queue = new PQueue({concurrency: 4});
590+
591+
const tasks = [
592+
() => fetchData(1), // May finish third
593+
() => fetchData(2), // May finish first
594+
() => fetchData(3), // May finish second
595+
];
596+
597+
const results = await Promise.all(
598+
tasks.map(task => queue.add(task))
599+
);
600+
// results = [result1, result2, result3] ✅ Always in input order
601+
602+
// Or more concisely:
603+
const urls = ['url1', 'url2', 'url3'];
604+
const results = await Promise.all(
605+
urls.map(url => queue.add(() => fetch(url)))
606+
);
607+
```
608+
609+
If you don't need `p-queue`'s advanced features, consider using [`p-map`](https://github.com/sindresorhus/p-map), which is specifically designed for this use case.
610+
582611
## Maintainers
583612

584613
- [Sindre Sorhus](https://github.com/sindresorhus)

0 commit comments

Comments
 (0)