neat-callbacks
v1.2.1
Published
A nifty little package that prevents callback hell (hopefully)
Downloads
1
Maintainers
Readme
neat callbacks
Before:
function before() {
console.log("A,");
setTimeout(() => {
console.log("B,");
}, 1000);
console.log("C.");
}
before();
// would output:
// A, C. B,
With neat-callbacks:
const after = NeatFunction.from(function* () {
console.log("A,");
yield Timeout(1000);
console.log("B,");
console.log("C.");
})
after();
// would output:
// A, B, C.
You have to imagine yield like a await
keyword, just outside of an async function.
For exemple:
// The default way to do it:
const request = new Promise((url) => {
fetch(url).then((data) => {
data.text.then((text) => {
resolve(text);
});
});
});
var text;
request("./text.txt").then(data => {
text = data;
});
// With neat-callbacks it is much cleaner;
var text;
const request = (function* (url) {
text = yield (yield fetch(url)).text;
}).toNeatFunction();
request("./text.txt");
// Or using XMLHttpRequest
const request = NeatFunction.from(function* (url, object, method = "get") {
var req = new XMLHttpRequest();
yield (resolve) => {
req.open(method, url, true);
req.send();
req.onload = () => {
resolve(req.responseText);
};
};
object = req;
});
Installing
npm i neat-callbacks
then, import it with require,
const { Timeout, NeatFunction } = require("neat-callbacks");
Or with
<script src="./node_modules/neat-callbacks/browser.js"></script>
Usage
NeatFunction.from
To create a neatFunction, you can use the function NeatFunction.from and passing a generator function as the first argument.
Please keep in mind that neat functions aren't async function, since they can't return any data.
Exemple:
NeatFunction.from(function* () {
// code
});
Timeout
As you might have noticed earlier, I used a helper function called Timeout
.
Timeout pauses the function for a set amount of milliseconds.
Exemple:
const timer = NeatFunction.from(function* (Seconds = 0) {
console.log("Timer started");
yield Timeout(Seconds * 1000);
console.log("Time's up!");
});
timer(3);
NeatFunction constructor
There is also a NeatFunction
constructor which allow to create a neat function the same way you would with the function constructor:
Exemples
// the last argument is always code, and the rest are arguments
new NeatFunction("a", "b",
"yield Timeout(1000);"+
"console.log(a+b);")
(1,2);
On node, if you want to run a file that uses yield in the global scope you use it like so:
new NeatFunction(fs.readFileSync("./test.js", "utf8"))();
What is yield-able
- promises
- async functions
- functions, the function will be called with the two first argument being
resolve
andreject
yield*
You can use the built-in yield*
keyword to use a iteratable.
NeatFunction.from(function* (callback) {
yield* [
promise1,
promise2,
promise3
]
// all three promises executed one after the other.
})
Or even
NeatFunction.from(function* (callback) {
yield* (function* {
for (let i = 0; i < 10; i++) {
yield i;
}
});
})
To receive data from the neat function:
const request = NeatFunction.from(function* (callback) {
// fetch your data
callback(data);
})
request(data => {
// do something with it
});
After developing this package I realised that something quite similar had already been done, check out co if this package doesn't have enough features for you.
Contributing
If you wish to contribute to this project, you can head over the github page and create a pull request. I'm not super active on github and npm, so don't panic if your pull request isn't seen
License
MIT