Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
The p-defer package is a utility for creating deferred promises in JavaScript. It allows you to create a promise and expose its resolve and reject functions, which can be called later to settle the promise. This is useful for scenarios where you need to control the timing of when a promise is resolved or rejected.
Creating a Deferred Promise
This feature allows you to create a deferred promise and resolve it at a later time. The `pDefer` function returns an object with a `promise` property and `resolve` and `reject` methods. You can use the `promise` property to await the promise and call `resolve` or `reject` to settle it.
const pDefer = require('p-defer');
const deferred = pDefer();
// Use the promise
async function example() {
await deferred.promise;
console.log('Promise resolved!');
}
example();
// Resolve the promise later
setTimeout(() => {
deferred.resolve();
}, 1000);
Handling Errors with Deferred Promises
This feature demonstrates how to handle errors with deferred promises. You can call the `reject` method to reject the promise with an error, and handle the error using a try-catch block when awaiting the promise.
const pDefer = require('p-defer');
const deferred = pDefer();
// Use the promise
async function example() {
try {
await deferred.promise;
} catch (error) {
console.error('Promise rejected:', error);
}
}
example();
// Reject the promise later
setTimeout(() => {
deferred.reject(new Error('Something went wrong'));
}, 1000);
The 'deferred' package provides similar functionality to p-defer by allowing you to create deferred promises. It also exposes the resolve and reject methods, but it includes additional features like progress notifications and chaining. It is more feature-rich compared to p-defer.
The 'promise-defer' package is another alternative that provides deferred promises. It is lightweight and straightforward, similar to p-defer, but it does not include any additional features beyond basic deferred promise functionality.
The 'bluebird' package is a fully-featured promise library that includes deferred promises among many other advanced features like cancellation, progress, and concurrency control. It is more comprehensive and powerful compared to p-defer, but also more complex.
Create a deferred promise
Don't use this unless you know what you're doing. Prefer the Promise
constructor.
npm install p-defer
import pDefer from 'p-defer';
function delay(milliseconds) {
const deferred = pDefer();
setTimeout(deferred.resolve, milliseconds, '🦄');
return deferred.promise;
}
console.log(await delay(100));
//=> '🦄'
The above is just an example. Use delay
if you need to delay a promise.
Returns an object
with a promise
property and functions to resolve()
and reject()
.
FAQs
Create a deferred promise
The npm package p-defer receives a total of 6,461,490 weekly downloads. As such, p-defer popularity was classified as popular.
We found that p-defer demonstrated a healthy version release cadence and project activity because the last version was released less than 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.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.