Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/allnulled/asynchandler
Callback adapter to use in a Promise.
https://github.com/allnulled/asynchandler
Last synced: 1 day ago
JSON representation
Callback adapter to use in a Promise.
- Host: GitHub
- URL: https://github.com/allnulled/asynchandler
- Owner: allnulled
- Created: 2020-06-08T14:14:29.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-06-08T14:26:12.000Z (over 4 years ago)
- Last Synced: 2024-11-10T02:23:14.302Z (8 days ago)
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/@allnulled/asynchandler
- Size: 1000 Bytes
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# asynchandler
Callback adapter to use in a Promise.
## Why
To adapt `callbacks paradigm` to `promises paradigm` in a nutshel.
## Installation
`$ npm i -s @allnulled/asynchandler`
## Usage
This example demonstrates how to use `asynchandler` with the node `fs` native module to read and write files using the asynchronous API:
```js
const asynchandler = require("@allnulled/asynchandler");const example = async function() {
await new Promise((ok, fail) => {
fs.writeFile("file.txt", "Contents of the file", "utf8", asynchandler(ok, fail));
});
await new Promise(function(ok, fail) {
fs.readFile("file.txt", "utf8", asynchandler(ok, fail));
});
};example();
```## Source code
The source code of this module is very simple:
```js
module.exports = function(ok, fail) {
return function(error, data) {
if (error) {
fail(error);
return;
}
ok(data);
return;
}
};
```## License
This project is released under `WTFPL` or `What The Fuck Public License`. Do what you want.