Product
Socket Now Supports uv.lock Files
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
A Lightning Fast, Yet Very Small Promise A+ Compliant Implementation
There are already several Promise implementations out there, and modern browsers even have built-in Promises, but none met my goals, which are:
Zousan is Promise A+ 1.1 compliant, so any documentation for spec-compliant promises applies to Zousan. There are a couple small additions though - see below. Briefly, the spec-compliant API is:
###Constructor
Create a new Promise (often, this promise is returned from a function that provides some asynchronous resource)
var promise = new Zousan(function(resolve, reject) {
// ... perform some asynchronous operation ...
// load the value to return into "value"
if(success)
resolve(value);
else
reject(Error("error message goes here"));
});
###then()
To use this promise to obtain the value:
promise.then(function(value) { // this function is called when promise is resolved
// do something with your value, you deserve it!
}, function(err) { // this function is called when promise is rejected
// bummer...
});
Zousan does have a couple additional features which are not required by the spec, but are very useful when working with promises. Be aware that if you use this "extension" API, your code may not be compatible with other Promise implementations:
###catch(errorFn)
catch(errorFn)
is equivalent to then(null, errorFn)
and is just easier to identify - allowing you to adopt the pattern of always ending then chains with a catch, like so:
getJSON("data.json") // hypothetical function which returns a promise
.then(lookupItems) // takes the data and obtains extra data about items
.then(updateCount) // update item count using host service
.then(displayResults) // update user view of results
.catch(reportErr); // Catch any errors occuring in any steps above.
This pattern helps you to remember to always catch any errors produced within your promise chains. Although this isn't part of the Promise A+, it is a very common addition and is present in the ES6 (ECMAScript 2015) draft spec.
###all(promiseArray)
The other addition is a utility function called all()
which takes an array of promises and returns a single promise that will resolve when all promises in the array resolve. The value passed to you is an array with the values from each promise respectively. If any promise within the passed array rejects, this will reject with the same error as the original rejection.
It is available by calling Zousan.all()
(i.e. does not require a promise instance).
For example, to obtain data from a list of sources:
// define an array with our data locations
var sources = ["data1.json", "data2.json", "data3.json"];
// Next, obtain an array of promises using hypothetical getJSON function
var dataProm = sources.map(getJSON);
// When all promises resolve, we call processData with array of results
Zousan.all(dataProm).then(processData, reportError);
This function is also present in the ECMAScript 2015 draft spec.
###Convenience resolve() / reject() as Instance Methods
The spec-compliant manner of resolving/rejecting a promise is to call the methods handed back to the constructor function argument, as shown in the constructor example above. Often it is more convenient (or cleaner) to resolve/reject a promise outside the constructor. For this purpose, Zousan provides resolve and reject methods right on the promise instance. So this pattern is available:
var promise = new Zousan();
if(success)
promise.resolve(value);
else
promise.reject(Error("error message goes here"));
###suppressUncaughtRejectionError flag
By default, Zousan will log a message to the console if a promise is rejected and that rejection is not "caught". Generally, it is best to use the catch()
pattern shown above, which will ensure all rejections are handled. If you forget, this will help remind you.
If you wish to suppress this warning, you can turn it off globally via:
Zousan.suppressUncaughtRejectionError = true;
Q: What does "Zousan" mean?
Well, if you had a 3-year-old Japanese child, you would know, now wouldn't you!? "Zou" is the Japanese word for "Elephant". "San" is an honorific suffix placed after someone's name or title to show respect. Children (and other kawaii people) often put "san" after animal names as a sign of respect to the animals.. and just to be kawaii.
Here is a video that might help
And if you need more guidance (or just enjoy these as much as I do) here is another - Zousan Da-ta!!
Q: Ok, cute - but why name it after an Elephant?
Because elephants never forget. So you can depend on them to keep their promises!
Q: Just how fast is it?
I set up a jsperf comparison between:
Note: Graph illustrates operations per second, so longer bars are better.
FAQs
A Lightning Fast, Yet Very Small Promise A+ Compliant Implementation
The npm package zousan receives a total of 945 weekly downloads. As such, zousan popularity was classified as not popular.
We found that zousan 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.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.
Security News
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.