antfu/utils

`p` method's `concurrency` config seems not working

Opened this issue · 0 comments

Describe the bug

I don't know if there's something wrong with the way I'm using it, but no matter how I assign a value to concurrency, all the Promises are always executed at the same time.

// main.ts

import { p } from "@antfu/utils";
import { loadData, sleep } from "./utils";

/**
 * Mock data loading
 * @returns 
 */
export async function loadData() {
  await sleep(Math.random() * 1000)
  return Math.round(Math.random() * 1000)
}

const concurrency = 1
let running = 0

p(
  Array.from({ length: 100 }).map(async (_, i) => {
    console.log(`start, running: ${running++}`)
    const data = await loadData();
    console.log(`end, running: ${running--}`)
    return data;
  }), 
  { concurrency }
)

The result of the above code is that all requests are executed simultaneously and end after a random time.
CleanShot 2023-06-20 at 19 10 43@2x

Reproduction

https://github.com/CatsJuice/mir-antfu-utils-p-not-working

System Info

System:
    OS: macOS 14.0
    CPU: (10) arm64 Apple M1 Pro
    Memory: 101.16 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.16.0 - ~/.nvm/versions/node/v18.16.0/bin/node
    Yarn: 1.22.17 - /usr/local/bin/yarn
    npm: 9.5.1 - ~/.nvm/versions/node/v18.16.0/bin/npm
  Browsers:
    Safari: 17.0

Used Package Manager

npm

Validations

  • Follow our Code of Conduct
  • Read the Contributing Guide.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.
  • The provided reproduction is a minimal reproducible of the bug.