node-moratorium
Delay a promise
Usage
const moratorium = ; { const text = 'print this after 1234 ms' await moratorium console}
API
moratorium.resolve(time, value)
Resolves a Promise after a specific amout of time.
Params:
time
-> time to wait
value
-> value returned by the Promise after the time set through time
Return: Promise
const value = await moratorium console // Prints Vitória after 1000 ms
moratorium.reject(time, value)
Rejects a Promise after a specific amout of time.
Params:
time
-> time to wait
value
-> value returned by the Promise after the time set through time
Return: Promise
try await moratorium // or await moratorium catch err console // After the delay: // In the frist case it prints Vitória, // in the second the Error object
moratorium.span.resolve(time, span, value)
Resolves a Promise after a randomic span of time.
Params:
time
-> time to wait
span
-> lower and upper bound added to time
(both bounds are inclusive)
value
-> value returned by the Promise after the time set through time
Return: Promise
const value = await moratoriumspanconsole // Prints Vitória after randomic time between 500 ms (inclusive) and 1500 ms (inclusive) const value = await moratoriumspanconsole // Prints Vitória after randomic time between 2200 ms (inclusive) and 2400 ms (inclusive)
moratorium.span.reject(time, span, value)
Rejects a Promise after a randomic span of time.
Params:
time
-> time to wait
span
-> lower and upper bound added to time
(both bounds are inclusive)
value
-> value returned by the Promise after the time set through time
Return: Promise
try await moratoriumspan // Rejects the promise after randomic time between 500 ms (inclusive) and 1500 ms (inclusive) await moratoriumspan // Rejects the promise after randomic time between 2200 ms (inclusive) and 2400 ms (inclusive) catch err console // After the delay: // In the frist case it prints Vitória, // in the second the Error object
moratorium.range.resolve(from, to, value)
Resolves a Promise after a randomic ranged amout of time.
Params:
from
-> lower bound (inclusive)
to
-> upper bound (inclusive)
value
-> value returned by the Promise after the time set through from
and to
Return: Promise
const value = await moratoriumrangeconsole // Prints Vitória after randomic time between 234 ms (inclusive) and 780 ms (inclusive) const value = await moratoriumrangeconsole // Prints Vitória after randomic time between 100 ms (inclusive) and 350 ms (inclusive)
moratorium.range.reject(from, to, value)
Rejects a Promise after a randomic ranged amout of time.
Params:
from
-> lower bound (inclusive)
to
-> upper bound (inclusive)
value
-> value returned by the Promise after the time set through from
and to
Return: Promise
try await moratoriumrange // Rejects the promise after randomic time between 500 ms (inclusive) and 1500 ms (inclusive) await moratoriumrange // Rejects the promise after randomic time between 100 ms (inclusive) and 350 ms (inclusive) catch err console // After the delay: // In the frist case it prints Vitória, // in the second the Error object
Promise properties and methods
Each Promise
object returned from the methods above has the following properties and methods:
promise.clear()
You can clear a delay by calling clear()
method in the Promise returned by one of the methods above.
const moratorium = ; { const text = 'print this after 500 instead of 1000' const delay = moratorium try await delay catch err console // If cleared with reason or error object console // If cleared without arguments}
promise.time
It's the time
that the Promise will have to wait before completion.
N.B: If the time
is negative, it will be set to 0.
const moratorium = ; { const delay = moratorium await delay console // Prints 1234}
promise.value
It's the value
that will be returned by the Promise.
const moratorium = ; { const delay = moratorium await delay console // Prints Vitória}