npm i promise-with-events
#or
yarn add promise-with-events
#or
pnpm add promise-with-events
import { createWatchEvent, onResolveEvents } from "promise-with-events";
const promiseExample1 = () => {
return new Promise((resolve) => {
setTimeout(resolve, 1000, "example1");
});
};
createWatchEvent(promiseExample1, {
eventName: "key1",
autoStart: true,
});
onResolveEvents(
(error, resolve1) => {
console.log(resolve1); //output = "example1"
},
["key1"]
);
- watch events
- start events
- events
- [advanced]
createWatchEvent
receive 2 params:
promises
: (() => promises)[]key
: that is the event nameOR
config
:
type TConfig = {
eventName: string;
autoStart?: boolean;
promiseMethod?: "all" | "allSettled" | "any" | "race";
};
Quick Examples
...
createWatchEvent(promise1, "key1")
...
createWatchEvent(promise1, {
eventName: "key1",
autoStart: true,
promiseMethod: 'race',
})
...
createWatchEvent([promise1, promise2], "key2")
...
createWatchEvent([promise1, promise2], {
eventName: "key2",
autoStart: true,
promiseMethod: 'race',
})
Example
import {
createWatchEvent,
startEvents,
onResolveEvents,
} from "promise-with-events";
const promiseExample1 = () => {
return new Promise((resolve) => {
setTimeout(resolve, 1000, "example1");
});
};
createWatchEvent(promiseExample1, "key1");
startEvents(["key1"]);
onResolveEvents(
(error, resolve1) => {
console.log(resolve1); //output = "example1"
},
["key1"]
);
Example
import {
createWatchEvent,
startEvents,
onResolveEvents,
} from "promise-with-events";
const promiseExample1 = () => {
return new Promise((resolve) => {
setTimeout(resolve, 1000, "example1");
});
};
const promiseExample2 = () => {
return new Promise((resolve) => {
setTimeout(resolve, 2000, "example2");
});
};
createWatchEvent([promiseExample1, promiseExample2], "key1");
startEvents(["key1"]);
onResolveEvents(
(error, resolve1) => {
console.log(resolve1); //output = ["example1", "example2"]
},
["key1"]
);
Example
import {
createWatchEvent,
startEvents,
onResolveEvents,
} from "promise-with-events";
const promiseExample1 = () => {
return new Promise((resolve) => {
setTimeout(resolve, 1000, "example1");
});
};
const promiseExample2 = () => {
return new Promise((resolve) => {
setTimeout(resolve, 2000, "example2");
});
};
const promiseExample3 = () => {
return new Promise((resolve) => {
setTimeout(resolve, 1000, "example3");
});
};
const promiseExample4 = () => {
return new Promise((resolve) => {
setTimeout(resolve, 2000, "example4");
});
};
createWatchEvent([promiseExample1, promiseExample2], "key1");
createWatchEvent([promiseExample3, promiseExample4], "key2");
startEvents(["key1", "key2"]);
onResolveEvents(
(error, resolve1, resolve2) => {
console.log(resolve1); //output = ["example1", "example2"]
console.log(resolve2); //output = ["example3", "example4"]
},
["key1", "key2"]
);
Note: startEvents
doesn't works with autoStart
active
Possible values: true
| false
(default value = true
)
Example
import { createWatchEvent, onResolveEvents } from "promise-with-events";
const promiseExample1 = () => {
return new Promise((resolve) => {
setTimeout(resolve, 1000, "example1");
});
};
const promiseExample2 = () => {
return new Promise((resolve) => {
setTimeout(resolve, 2000, "example2");
});
};
createWatchEvent([promiseExample1, promiseExample2], {
eventName: "key1",
autoStart: true,
});
onResolveEvents(
(error, resolve1) => {
console.log(resolve1); //output = ["example1", "example2"]
},
["key1"]
);
Note: promiseMethod
doesn't works with single promise
Possible values: all
| allSettled
| any
| race
(default value = all
)
Example
import { createWatchEvent, onResolveEvents } from "promise-with-events";
const promiseExample1 = () => {
return new Promise((resolve) => {
setTimeout(resolve, 1000, "example1");
});
};
const promiseExample2 = () => {
return new Promise((resolve) => {
setTimeout(resolve, 2000, "example2");
});
};
createWatchEvent([promiseExample1, promiseExample2], {
eventName: "key1",
autoStart: true,
promiseMethod: "any",
});
onResolveEvents(
(error, resolve1) => {
console.log(resolve1); //output = "example1"
},
["key1"]
);
Note: Doesn't works with autoStart: true
Example
import { startEvents } from "promise-with-events";
startEvents(["key1"]);
startEvents(["key1", "key2"]);
All events
receive 2 params:
callback
: returns error, ...responses(response for each key)
keys
: string[]
Note: error
on callback its about code errors
Calls callback
param when all promises are resolved
Note: Doesn't works with autoStart: true
Quick Examples
...
onStartEvents((error) => {
console.log('ok')
}, ["key1"])
...
onStartEvents((error) => {
console.log('ok')
}, ["key1", "key2"])
Calls callback
param when all promises are resolved
Quick Examples
...
onResolveEvents((error, resolve1) => {
console.log(resolve1)
}, ["key1"])
...
onResolveEvents((error, resolve1, resolve2) => {
console.log(resolve1)
console.log(resolve2)
}, ["key1", "key2"])
Calls callback
param when all promises are rejected
Quick Examples
...
onRejectEvents((error, reject1) => {
console.log(reject1)
}, ["key1"])
...
onResolveEvents((error, reject1, reject2) => {
console.log(reject1)
console.log(reject2)
}, ["key1", "key2"])
Calls callback
param when all promises are finished
Quick Examples
...
onFinallyEvents((error, finally1) => {
console.log(finally1.response) //could be undefined, according promise result
console.log(finally1.error) //could be undefined, according promise result
}, ["key1"])
...
onFinallyEvents((error, finally1, finally2) => {
console.log(finally1.response)
console.log(finally1.error)
console.log(finally2.response)
console.log(finally2.rerror)
}, ["key1", "key2"])
Example
import { createWatchEvent, onResolveEvents } from "promise-with-events";
const promiseExample1 = () => {
return new Promise((resolve) => {
setTimeout(resolve, 1000, "example1");
});
};
const promiseExample2 = () => {
return new Promise((resolve) => {
setTimeout(resolve, 2000, "example2");
});
};
const promiseExample3 = () => {
return new Promise((resolve) => {
setTimeout(resolve, 1000, "example3");
});
};
const watchedPromise1 = createWatchEvent([promiseExample1, promiseExample2], {
eventName: "key1",
autoStart: true,
promiseMethod: "any",
});
const watchedPromise2 = createWatchEvent([watchedPromise1, promiseExample3], {
eventName: "key2",
autoStart: true,
});
createWatchEvent([watchedPromise1, watchedPromise2], {
eventName: "key3",
autoStart: true,
promiseMethod: "all",
});
onResolveEvents(
(error, resolve1) => {
console.log(resolve1); //output = "example1"
},
["key1"]
);
onResolveEvents(
(error, resolve2) => {
console.log(resolve2); //output = ["example1", "example3"]
},
["key2"]
);
onResolveEvents(
(error, resolve3) => {
console.log(resolve3); //output = ["example1", ["example1", "example3"]]
},
["key3"]
);
To contribute, make sure to follow the steps bellow:
-
Create a new branch:
git checkout -b feat/your-new-feature
-
Make your changes, add unit tests (with
jest
) and test withnpm link
On promise-with-events project:
npm link
On your app/project:
npm link promise-with-events
This will create a symlink into your
node_modules
app, and you can test iteratively. You can check more about npm-link here -
Before to push your changes to origin, open your pull request and fill all required fields.
- Make sure to fill the Release section with what your pull request changes. This section is required to merge pull request.
-
Set a required
semver
label according to your change:semver:patch
: used when you submit a fix to a bug, enhance performance, etc;semver:minor
: used when you submit a new component, new feature, etc;semver:major
: used when you submit some breaking change, etc;semver:prerelease
: used when you submit a prerelease (ex:1.0.0-beta.1
);semver:bypass
: used to update docs, or something that doesn’t affect the build.
Info: Once you have merged your pull request, with all required fields, GitHub Actions will be responsible to create a new build and publish.
MIT License