
Research
/Security News
Weaponizing Discord for Command and Control Across npm, PyPI, and RubyGems.org
Socket researchers uncover how threat actors weaponize Discord across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.
capture-promise
Advanced tools
Small utility function for safe evaluation of async functions, capturing synchronous outcomes as well
The problem: you want to call a user-supplied callback and expect it to return a Promise, but you don't know if it might throw or return synchronously, and you want to make sure that you are dealing with a Promise.
This solves that problem by capturing all outcomes of a given callback - whether they are synchronous or asynchronous - and always giving you a Promise that resolves or rejects accordingly, much like you would expect an async
function or .then
to do.
Roughly analogous to Promise.try
.
A runnable version of this example is included in the package as example.js
.
"use strict";
const capturePromise = require("capture-promise");
function dubiousUserSuppliedCallback() {
if (Math.random() < 0.5) {
return Promise.resolve(true);
} else {
throw new Error(`Oops, this is synchronously thrown!`);
}
}
(async function () {
let promise = capturePromise(() => dubiousUserSuppliedCallback());
console.log(promise); // Always prints a Promise, regardless of whether the callback throws or not
await promise; // ... and we can await it like any Promise!
})();
Returns: A Promise that resolves if the callback returned a resolved Promise or synchronous value; or rejects if the callback returned a rejected Promise or threw a synchronous error.
FAQs
Small utility function for safe evaluation of async functions, capturing synchronous outcomes as well
We found that capture-promise demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
Socket researchers uncover how threat actors weaponize Discord across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.
Security News
Socket now integrates with Bun 1.3’s Security Scanner API to block risky packages at install time and enforce your organization’s policies in local dev and CI.
Research
The Socket Threat Research Team is tracking weekly intrusions into the npm registry that follow a repeatable adversarial playbook used by North Korean state-sponsored actors.