Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/szhu/threads
Stateful JavaScript serial async management.
https://github.com/szhu/threads
Last synced: about 1 month ago
JSON representation
Stateful JavaScript serial async management.
- Host: GitHub
- URL: https://github.com/szhu/threads
- Owner: szhu
- License: mit
- Created: 2014-06-29T16:53:12.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2014-07-04T23:03:15.000Z (over 10 years ago)
- Last Synced: 2024-04-11T15:52:57.923Z (9 months ago)
- Language: CoffeeScript
- Size: 164 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
threads
=======Stateful JavaScript serial async management.
Threads gives functions in a serial async chain control of the entire flow with a `Thread` object and access to a thread-wide namespace, `Thread#vars`. Threads gives you a superset of controls that are possible with callbacks, but it plays nicely with callbacks too. Simply pass `Thread#continue` as a callback to traditional async functions.
```coffee
fetchSomeDataWithCallback = (who, callback) ->
if who == 'you' then callback 'pretty cool'
else callback 'not as cool't = new Thread
t.run [
# STEP 1
->
setTimeout ->
t.vars.who = 'you'
t.continue()
, 500
# STEP 2
->
fetchSomeDataWithCallback(t.vars.who, t.continue)
# STEP 3
(returnedData) ->
console.log "results of computation: #{t.vars.who}: #{returnedData}"
t.continue()
]
```Always forgetting to write callbacks? Threads keeps track of unfinished threads, allowing you to figure out which callbacks are missing.
```coffee
t = new Thread 'thread-that-forgot-to-finish'
t.run -> # do nothing, conveniently forgetting to t.continue()# Hm, did we forget to finish executing any threads?
console.log "#{Thread.current.length} threads haven't finished!"
console.log "The first one is named #{Thread.current[0]?.name}."
console.log "Perhaps you should go fix that."
```Threads is curently written in CoffeeScript, but of course you can compile it all to JavaScript.