Compact implementation of Promise/A+
Compact Promise is a lightweight Promise/A+ compliant implementation with very small footprint (870 bytes for smallest compilation after minified and gzipped).
References
Promise.onError
- Overwrite this property with a function so that the function gets called whenever an error or exception is detected.Promise(func)
- constructor of a promise,func
will be called once instantiation is done with 2 functions as its parameters --resolve
andreject
. Call each function respectly to resolve or reject the promise.Promise.prototype.then(resolveCallback, rejectCallback)
- InvokeresolveCallback
when the promise is resolved, the vice versa forrejectCallback
.
Promise.all(promises)
- Resolve when the all the promises in thepromises
array are resolved, reject when any of thepromises
is rejected.Promise.resolve(promise)
- Return a resolved promise whenpromise
is resolved or null.Promise.reject(reason)
- Return a reject promise with reason as its error.Promise.Defer()
- Constructor of defer.Promise.Defer.prototype.resolve(value)
- Resolve the defer withvalue
.Promise.Defer.prototype.reject(error)
- Reject the defer witherror
.
Compilation and Promise/A+ Compliant
The complete version of Compact Promise is fully compliant with Promise/A+. However lots of developers may find that its unnecessary to bring in all the standard and extended features into the project, as most of them probably never used.
To reduce the extra fat, Compact Promise is also compiled with/without certain function sets so that you can pick up just what you need for your next project.
Here is list of compliancy of each compilations:
- Default - full compliant, with extension methods such as Defer.all()
- notick - Promise/A+ 3.1 is excluded to avoid to implement platform specific micro-task
nextTick
or macro-tasksetImmediate
, as a lot of developers has already pointed out, the cross platform implementation of those, at the moment is bloated with feature detections, workarounds and hacks, which means it doesn't fit the ideology of being a compact, lightweight, use anywhere library. And even so, it still doesn't support a lot of older platforms and mobile platforms and at those cases it fallbacks tosetTimeout
, which means very slow execution and performance. By not implementing Promise/A+ 3.1, in a lot of cases, its not just make page loads faster, run faster, but also avoid unnecessary async calls so it makes debugging a lot of easier as well. - noext - No extension method such as Defer.all(), these methods are not part of the standard, they are added because they are very common in the other similiar libs.
- noumd - No UMD header, plain Javascript!
install with NPM or Bower
npm install compact-promise
bower install compact-promise --save
Tests
To run full tests
npm run test
To run Promise/A+ tests
npm run test-basic
To run extended tests
npm run test-ext