PaxJS Promise
Global Promise
object improvements (zero-dependency, 1Kb).
Why
There is a great library bluebird
, which provides a similar functionality, but often you need only part of it and it's much easier to use it as global Promise
object methods, than import it in every module where you need it.
paxjs-promise
works everywhere where global Promise
object is available. In Internet Explorer it does nothing.
Installation
Open a Terminal in your project's folder and run,
npm install paxjs-promise
Usage
Import paxjs-promise
at start of your app entry point module. After that global Promise
object will be enhanced and its new methods will be available in all your code.
or
If you need only part of paxjs-promise
functionality, you can use it like this, without using the whole library.
Promise.delay
The same as bluebird
Promise.delay
Promise.delay int ms, -> Promise
Returns a promise that will be resolved with value
(or undefined
) after given ms
milliseconds. If value
is a promise, the delay will start counting down when it is fulfilled and the returned promise will be fulfilled with the fulfillment value of the value
promise. If value
is a rejected promise, the resulting promise will be rejected immediately.
Use cases
It's often in your app UX you have states that are limited in time (e.g., request success and failure feedbacks). Promise.delay
gives you short and neat syntax how to handle such cases.
// raw promises
{ }
// async/await
async { try await catcherr finally await Promise }
Another case is when you have additional request after your main request and you don't want to overload the app after your main request is finished to not slow down its feedback rendering. You can do like this
// raw promises
{ }
// async/await
async { try await await Promise catcherr ... }
You can use Promise.delay
to execute code in next tick.
async { this await Promise console // 'Hello World!'}
is equivalent of
{ this }
When you call Promise.delay
without ms
argument, it falls to just Promise.resolve
You can pass value or another promise as a second argument in Promise.delay
.
{ return Promise}
Promise.prototype.time
Promise.prototype.time -> Promise
Adds min
and max
time to promise. If min
time is specified, the resulting promise will be resolved not before given milliseconds. If min
time is specified and promise is rejected, the resulting promise will be rejected immediately. If max
time is specified and promise takes more time to resolve, the resulting promise will be rejected with Promise.TimeError
. max
can't be less than min
, else the resulting promise will be rejected immediately with Promise.TimeError
Use cases
In UX you often need to set minimum and maximum time for "in-progress" state (e.g., 300ms and 3000ms). With Promise.prototype.time
you can do it like this
async { try await catcherr console if err instanceof PromiseTimeError else }
In Node.js sometimes you need to limit maximum time for request to external service
app