Shortcut for Promise.all, with the ability to pass the promises as single arguments.
import { all, ok } from "tilly";
const result = await all(
ok("Result #1"),
ok("Result #2"),
ok("Result #3")
);
console.log(result);
// ["Result #1", "Result #2", "Result #3"]
The first executor function or Promise to resolve.
The list of executors or Promises to be solved can be passed as individual arguments or as an Array in the first argument.
Generate a function that invoke a generator parameter and return the same Promise until this reject or if is elapsed a specified time
import { cache, ok, ko } from "tilly";
const getResolved = cache(() => ok("Result!"));
const prom1 = getResolved();
// wait Promise resolves/rejects…
const prom2 = getResolved();
console.log(prom1 === prom2);
// true
const getRejected = cache(() => ko("Error!"));
const prom3 = getRejected();
// wait Promise resolves/rejects…
const prom4 = getRejected();
console.log(prom3 === prom4);
// false
The function that generate the promise to cache
Function that allows you to execute a promise followed by a sequence of transformations of the returned value through a chain of calls to the then() method.
import { chain } from "tilly";
const result = await chain(
(ok, ko) => {
ok(2);
},
(value) => {
return value * 2;
},
(value) => {
return value + 3;
}
);
console.log(result);
// 7
Function/Promise/mixed The Promise, or the executor function for the Promise, or value for the resolved promise
Result Promise of the chain calls
This function delay the resolution of a promise.
De facto is an alias of sleep() function with inverse not-optional arguments
import { delay } from "tilly";
const result = delay("wake up!", 3000);
console.log(result);
// "wake up!"
Promise, or executor function, or data value to pass to sleep() resolution after waiting time expires
The time to wait before Promise resolution, in milliseconds
Similar to the "all" function but with the "to" function applied to each element.
This allows to obtain the result of each Promise, both in case of resolution and rejection.
import { every, ok, ko } from "tilly";
const result = await every(
ok("Result #1"),
ko("Error #2"),
ok("Result #3")
);
console.log(result);
// [{"success": true, "payload": "Result #1"},
// {"success": false, "error": Error("Error #2")},
// {"success": true, "payload": "Result #3"}]
List of executors or Promises to be solved can be passed as individual arguments or as an Array in the first argument.
Result of all Promises
Shortcut for Promise initialization.
import { from } from "tilly";
const result = await from((ok, ko) => {
ok("Resolved!");
});
console.log(result);
// "Resolved!"
The Promise, or the executor function, or the value of resolved promise
Shortcut for Promise.reject.
import { ko } from "tilly";
const result = await ko("Something went wrong");
// Error("Something went wrong")
Rejected promise
Shortcut for Promise.resolve.
import { ok } from "tilly";
const result = await ok("Resolved!");
console.log(result);
// "Resolved!"
The resolved result of the Promise
Resolved promise
Shortcut for Promise.race, with the ability to pass the promises as single arguments.
import { race, ok } from "tilly";
const result = await race(
(resolve) => { setTimeout(() => { resolve("One"); }, 600); },
(resolve) => { setTimeout(() => { resolve("Two"); }, 200); },
(resolve) => { setTimeout(() => { resolve("Three"); }, 400); },
);
console.log(result);
// "Two"
The first executor function or Promise to resolve.
The list of executors or Promises to be solved can be passed as individual arguments or as an Array in the first argument.
The first resolved Promise
This function allows you to execute the call to Promise several times in the case of re-rejection, until there is a resolution or the attempts are exhausted.
import { retry } from "tilly";
const result = await retry(3, (resolve, reject, i, n) => {
if(i == n) {
resolve("At the last attempt");
} else {
reject(new Error("Failed attempt"));
}
});
console.log(result);
// "At the last attempt"
The maximum number of attempts to retry to execute the Promise
The executor function with the addition of two arguments, the attempt index and the total number of attempts
Result Promise
This function in conjunction with the use of await allows pausing the execution of the current context.
Without the use of await it allows you to perform a Promise with delay.
import { sleep } from "tilly";
const result = await sleep(3000, "wake up!");
console.log(result);
// "wake up!"
The time to wait before Promise resolution, in milliseconds
Optional. Promise, or executor function, or data value to pass to sleep() resolution after waiting time expires
Function that allows the safe use of await without the need for a try / catch. In case of rejection the error is captured and returned.
The structure of the result of the function is an object with two properties: "success" true / false and depending on the case "payload" or "error" evaluated.
import { to, ok, ko } from "tilly";
const result1 = await to(
ok("Resolved!")
);
console.log(result1);
// {"success": true, "payload": "Resolved!"}
const result2 = await to(
ko("Something went wrong")
);
console.log(result2);
// {"success": false, "error": Error("Something went wrong")}
The Promise to manage
Promise with managed result object
Generated using TypeDoc
tilly