Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tarabyte/fuq
Functional Queue <= 140 chars
https://github.com/tarabyte/fuq
Last synced: about 2 months ago
JSON representation
Functional Queue <= 140 chars
- Host: GitHub
- URL: https://github.com/tarabyte/fuq
- Owner: Tarabyte
- License: mit
- Created: 2014-05-11T16:51:30.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2014-05-12T08:21:22.000Z (over 10 years ago)
- Last Synced: 2024-04-24T22:31:23.220Z (8 months ago)
- Language: JavaScript
- Size: 141 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
FuQ
===Functional Queue in <= 140 chars of code. Actually it is 122 now, so there is 18 symbols more you can add. :)
```javascript
FuQ=function(c,o){return{run:function r(d){for(o=d;(d=c.shift())&&!((o=d(o))&&o.q&&o.q(r)););},q:{q:(c=[]).push.bind(c)}}}
```Library provides an easy way to chain asynchrnous function calls.
Example
=====Demonstrates asynchronous nested queues.
Logs:* `Hello, world!` in a second.
* `Nested: Hello, world!` in 2 seconds.
* `Nested: Nested: Hello, world!` immediately after the second one.```javascript
function later(timeout, data) {
var dfd = FuQ();
setTimeout(function(){
dfd.run(data); //run all added functions
}, timeout)
return dfd.q; //return a queue;
}function tap(f) { //make any function to return its first argument
return function(data){
f.apply(this, arguments);
return data;
}
}var log = tap(console.log.bind(console)); //make console.log to return its first argument
later(1000, 'Hello, world!').q(log, function(msg){
var q = later(2000, 'Nested: ' + msg);
q.q(log, function(msg) { //log and process a message
return 'Nested: ' + msg;
});
return q; //return a nested async queue
}, log);```