{"library":"run-async","title":"Unified Async/Sync Function Execution","description":"run-async is a utility method designed to normalize the execution of functions that can operate synchronously, asynchronously via a `this.async()` callback, or by returning a Promise. This library is particularly useful for authors of middleware or plugins that need to accept user-provided functions with varying asynchronous patterns, ensuring a consistent execution flow. The current stable version is 4.0.6, and the project demonstrates an active maintenance cadence, addressing dependency issues and enhancing functionality across major versions. A key differentiator is its ability to abstract away the underlying async mechanism, providing a single interface, and its current status as a dependency-free package since version 2.4.1, which improves reliability and reduces supply chain risks.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install run-async"],"cli":null},"imports":["import { runAsync } from 'run-async';","import { runAsync } from 'run-async'; runAsync.cb(...);","import type { RunAsync } from 'run-async';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { runAsync } from 'run-async';\n\n// A helper to demonstrate how runAsync normalizes different function types\nconst printAfter = async (func: Function) => {\n  const cb = (err: Error | null, returnValue: unknown) => {\n    if (err) {\n      console.error('Error:', err);\n      return;\n    }\n    console.log(returnValue);\n  };\n\n  // Execute the function using runAsync, passing a callback\n  // runAsync returns a function that takes arguments for func\n  try {\n    const result = await runAsync(func, cb)();\n    // If func returns a Promise, runAsync resolves it. The callback also fires.\n    // We'll catch the potential Promise resolution here.\n    // The callback provided to runAsync will still be called.\n  } catch (error) {\n    console.error('Caught an error from runAsync execution:', error);\n  }\n};\n\nconsole.log('--- Using this.async ---');\nprintAfter(function (this: any) {\n  const done = this.async();\n  setTimeout(() => {\n    done(null, 'done running with callback');\n  }, 10);\n});\n\nconsole.log('\\n--- Returning a Promise ---');\nprintAfter(function () {\n  return new Promise(resolve => {\n    setTimeout(() => resolve('done running with promises'), 5);\n  });\n});\n\nconsole.log('\\n--- Synchronous function ---');\nprintAfter(function () {\n  return 'done running sync function';\n});\n\n// Example using runAsync.cb for Node.js callback style\nconsole.log('\\n--- Using runAsync.cb (Node.js callback style) ---');\nrunAsync.cb(\n  (a: number, b: number, cb: (err: Error | null, result: number) => void) => {\n    setTimeout(() => cb(null, a + b), 20);\n  },\n  (err: Error | null, result: number) => {\n    if (err) console.error('Error with runAsync.cb:', err);\n    else console.log(`runAsync.cb result: ${result}`);\n  }\n)(5, 7);\n","lang":"typescript","description":"Demonstrates how to use `runAsync` to execute functions that use `this.async()`, return a Promise, or return a synchronous value. Also includes an example of `runAsync.cb` for Node.js callback-style functions.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}