Watch promises whether all settled or not
npm install promise-watchThis module watches promise objects which have been made, and tells whether all promises have been settled or not.
> A Promise is in one of these states:
> ・pending: initial state, neither fulfilled nor rejected.
> ・fulfilled: meaning that the operation completed successfully.
> ・rejected: meaning that the operation failed.
> Note: A promise is said to be settled if it is either fulfilled or rejected, but not pending.
> Promise - JavaScript | MDN
npm install promise-watch
`Usage example
https://higuri.github.io/promise-watch/$3
`javascript
const {PromiseWatch} = require("promise-watch");function log(textarea, msg) { ... }
async function start3SecTask() { ... }
// loader: loading icon
const loader = document.querySelector(...);
// textarea: status lines
const textarea = document.querySelector(...);
// button: start a task
const button = document.querySelector(...);
// create a watch instance
const watch = new PromiseWatch();
// subscribe 'added' event
watch.onAdded(() => {
if (loader.classList.contains("hidden")) {
loader.classList.remove("hidden");
}
});
// subscribe 'allSettled' event
watch.onAllSettled(() => {
loader.classList.add("hidden");
});
button.addEventListener("click", async() => {
log(textarea, "Start a task...");
// run an asynchronous task through watch.run()
await watch.run(start3SecTask());
log(textarea, "Task finished !!");
});
`
full versionclass
PromiseWatch
$3
Example:
`javascript
const watch = new PromiseWatch();
`$3
Example:
`javascript
watch.run(asyncFunctionFoo(...))
.then(() => {
// lines to be executed after Foo() ...
});
`
`javascript
await watch.run(someAsyncFunctionFoo(...));
// lines to be executed after Foo() ...
`
`javascript
watch.run(someAsyncFunctionFoo(...));
// lines to be executed in asynchronously with Foo() ...
`$3
Example:
`javascript
watch.onAdded(() => {
console.log("There are one or more pending tasks!");
});
`$3
Example:
`javascript
watch.onAllSettled(() => {
console.log("There are no pending tasks!");
});
`$3
Example:
`javascript
watch.onOneSettled(() => {
console.log("A promise is fulfilled or rejected!");
});
`$3
Example:
`javascript
console.log(Number of pending tasks: ${watch.pendings.length});
``