Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
@braintree/extended-promise
Advanced tools
Readme
Promises are great! But, could they be even better?
npm install @braintree/extended-promise
This library was developed to make working with APIs that are not easily wrapped in promises, such as kicking off a promise that resolves within a callback for an event listener.
Before we wrote this lib, we were saving references to the underlying promise, and references to the resolve
/reject
functions in order to resolve and reject it later:
// without this lib
class MyCustomObject {
constructor() {
this._promise = new Promise((resolve, reject) => {
this._resolveFunction = resolve;
this._rejectFunction = reject;
});
}
asyncProcess() {
// do something very async that's not easily wrapped in a promise
if (success) {
this._resolveFunction(data);
} else {
this._rejectFunction(new Error("fail"));
}
}
methodRelyingOnResultOfPromise() {
return this._promise;
}
}
Instead, we can save a reference to just the promise, and then resolve or reject it directly:
// with this lib
class MyCustomObject {
constructor() {
this._promise = new ExtendedPromise();
}
asyncProcess() {
// do something very async that's not easily wrapped in a promise
if (success) {
this._promise.resolve(data);
} else {
this._promise.reject(new Error("fail"));
}
}
methodRelyingOnResultOfPromise() {
return this._promise;
}
}
The object also supplies an onResolve
and onReject
hook that can be passed in on instantiation:
var promise = new ExtendedPromise({
onResolve(data) {
return someFunctionToTransformData(data);
},
onReject(error) {
// decide if you want to catch the error
return someFallbackDataInstead;
// or if you want to continue through with the rejection
return Promise.reject(error);
},
});
One of the benefits of using ExtendedPromise
is that you can pass the promise object around and catch the errors asyncronously. However, this can result in Node or the browser assuming that when the promise rejects before you've added your rejection handler, that the promise is completely undhandled. To suppress these warnings, add the suppressUnhandledPromiseMessage
option when instantiationg the promise:
var promise = new ExtendedPromise({
suppressUnhandledPromiseMessage: true,
});
// do async stuff that results in the promise rejecting
// and don't worry about Node or the browser being
// angry about it
promise.catch(function (err) {
// handle this later
});
You can also set this globally for all ExtendedPromise
s:
ExtendedPromise.suppressUnhandledPromiseMessage = true;
var promise = new ExtendedPromise();
// do async stuff that results in the promise rejecting
// and don't worry about Node or the browser being
// angry about it
promise.catch(function (err) {
// handle this later
});
If both the global property and the instance property are set, it will use the instance property.
When ExtendedPromise
is instantiated with a function, it will return the underlying Promise. The resolve
and reject
instance methods should not be used with this method. This is simply to make the migration to using ExtendedPromise easier.
var promise = new ExtendedPromise(function (resolve, reject) {
// if success
resolve('value');
// if failure
reject(new Error('err'));
});
promise.then(val => {
// handle result
}).catch(err => {
// handle error
})
If your environment does not support Promises, you can set the Promise object to be used directly on the Object.
const PromisePolyfill = require("promise-polyfill");
ExtendedPromise.setPromise(PromisePolyfill);
Run tests:
npm test
FAQs
Promises are great! But, could they be even better?
The npm package @braintree/extended-promise receives a total of 165,671 weekly downloads. As such, @braintree/extended-promise popularity was classified as popular.
We found that @braintree/extended-promise 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
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.