| Kaido Kert | f585e26 | 2020-06-08 11:42:28 -0700 | [diff] [blame] | 1 | # dezalgo |
| 2 | |
| 3 | Contain async insanity so that the dark pony lord doesn't eat souls |
| 4 | |
| 5 | See [this blog |
| 6 | post](http://blog.izs.me/post/59142742143/designing-apis-for-asynchrony). |
| 7 | |
| 8 | ## USAGE |
| 9 | |
| 10 | Pass a callback to `dezalgo` and it will ensure that it is *always* |
| 11 | called in a future tick, and never in this tick. |
| 12 | |
| 13 | ```javascript |
| 14 | var dz = require('dezalgo') |
| 15 | |
| 16 | var cache = {} |
| 17 | function maybeSync(arg, cb) { |
| 18 | cb = dz(cb) |
| 19 | |
| 20 | // this will actually defer to nextTick |
| 21 | if (cache[arg]) cb(null, cache[arg]) |
| 22 | |
| 23 | fs.readFile(arg, function (er, data) { |
| 24 | // since this is *already* defered, it will call immediately |
| 25 | if (er) cb(er) |
| 26 | cb(null, cache[arg] = data) |
| 27 | }) |
| 28 | } |
| 29 | ``` |