You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js async.parallelLimit报错TypeError: callback is not a function求助

Fixing TypeError: callback is not a function in async.parallelLimit

Hey there! Let's break down why you're hitting this error and how to fix it.

What's Causing the Error?

You're passing async functions into async.parallelLimit, but the traditional callback-based version of async.js doesn’t play nicely with async/await here.

Here’s the core issue: Your task functions are defined as async function(callback) { ... }, but when async.parallelLimit runs these functions, it doesn’t actually pass a callback argument to them (since it expects callback-style functions, not Promise-returning async functions). So when you try to call callback(null, true) inside your async function, callback is undefined—hence the TypeError.

How to Fix It

You have two solid options to resolve this:

Option 1: Use Callback-Style Functions (No Async/Await)

Rewrite your task functions to fit the traditional async.js callback pattern, replacing await with .then()/.catch() to handle the API promise:

api_call_arr.push(function(callback) {
  apiHelper.makeAPI('post', api_data)
    .then(api_result => {
      if (api_result.Errors !== undefined) {
        console.log('success 1');
      } else {
        console.log('error 1');
      }
      callback(null, true);
    })
    .catch(err => {
      console.error('API call failed:', err);
      callback(err);
    });
});

async.parallelLimit(api_call_arr, 5, function(err, data) {
  console.log(err);
});

Option 2: Use Async.js's Promise-Based API

If you prefer to keep using async/await, switch to async.js's promise-compatible API (available in async@3.x or later, or via the async-es package). This lets you pass Promise-returning functions directly, no callbacks needed:

// For CommonJS
// const { parallelLimit } = require('async');
// For ES Modules
// import { parallelLimit } from 'async';

api_call_arr.push(async function() {
  let api_result = await apiHelper.makeAPI('post', api_data);
  if (api_result.Errors !== undefined) {
    console.log('success 1');
  } else {
    console.log('error 1');
  }
  return true; // Return the result instead of calling callback
});

// Use promise chaining instead of a final callback
parallelLimit(api_call_arr, 5)
  .then(data => {
    console.log('All calls completed:', data);
  })
  .catch(err => {
    console.error('Error in parallel calls:', err);
  });

Key Takeaway

The traditional callback-based async.js methods don’t support async/await functions directly because they don’t inject the callback argument into async functions. Stick to callback-style functions for the old API, or upgrade to the promise-based API to use async/await smoothly.

内容的提问来源于stack exchange,提问作者Kien Duong

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:38:03