question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

mapLimit without callback only executes "limit" amount of times

See original GitHub issue

What version of async are you using? 3.1.0

Which environment did the issue occur in (Node version/browser version) node: 10.16.0

What did you do? Please include a minimal reproducible case illustrating issue.

"use strict";
const delay = (timeout) => {
    return new Promise((resolve) => {
        setTimeout(resolve, timeout);
    });
};
const myAsyncFunction = async function () {
    return async.mapLimit([1, 2, 3, 4, 5], 3, async (num) => {
        await delay(2000);
        console.log(num * 2);
        return num * 2;
    });
};
myAsyncFunction().then((result) => {
    console.log(result);
});

https://codepen.io/bertyhell/pen/NWKMRvY?editors=0012

What did you expect to happen? The code should output: 2, 4, 6, 8, 10 Then at the end of the mapLimit the code should log the array: [2, 4, 6, 8, 10]

What was the actual result? The code outputs: 2, 4, 6 The final then is never called, the await on the async.mapLimit just hangs

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:9

github_iconTop GitHub Comments

1reaction
bertyhellcommented, Sep 25, 2019

Yes, then it works: tsconfig.json:

{
  "compilerOptions": {
    "target": "es2017"
  }
}

This means async v3 isn’t compatible with typescript for targets < es2017 😭

I thought you detected an async function because no callback method is passed.

Or would that cause breaking changes for people that didn’t pass a callback, but still expected the callback version of the async lib function to be used?

1reaction
aearlycommented, Sep 24, 2019

If typescript is compiling async functions down to generator function, then Async can’t tell if it’s an async function or a callback-accepting function. Can you tell TS to target ES2017 rather than ES2015?

The other (ugly) solution is to wrap async functions with asyncify:

    return async.mapLimit([1, 2, 3, 4, 5], 3, asyncify(async (num) => {
        await delay(2000);
        console.log(num * 2);
        return num * 2;
    }));
Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - What's point of mapLimit? - Stack Overflow
Without mapLimit node will make all the requests at once (if you're ... Just limit number of async operations at a time, so...
Read more >
How to use the async.mapLimit function in async - Snyk
To help you get started, we've selected a few async examples, based on popular ways it is used in public projects. ; executeTests(manifest,...
Read more >
async - Documentation - GitHub Pages
A callback which is called after all the iteratee functions have finished, ... The same as concat but runs a maximum of limit...
Read more >
How to use Async module to control API requests.
We attach a callback and leave the task to complete the execution. ... Limit to perform the maximum number of operations at a...
Read more >
async
mapLimit (arr, limit, iterator, callback). The same as map only no more than "limit" iterators will be simultaneously running at any time.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found