This is just a copy of the wait.for implementation of luciotato/Wait.for but in Typescript.
Sequential programming for node.js, end of callback hell. Simple, straightforward abstraction over Fibers.
By using wait.for, you can call any nodejs standard async function in sequential/Sync mode, waiting for result data, without blocking node's event loop (thanks to fibers)
A nodejs standard async function is a function in which the last parameter is a callback: function(err,data)
Advantages:
- Avoid callback hell / pyramid of doom
- Simpler, sequential programming when required, without blocking node's event loop (thanks to fibers)
- Simpler, try-catch exception programming. (default callback handler is: if (err) throw err; else return data)
- You can also launch multiple parallel non-concurrent fibers.
- No multi-threaded debugging nightmares, only one fiber running at a given time (thanks to fibers)
- Can use any node-standard async function with callback(err,data) as last parameter.
- Plays along with node programming style. Write your async functions with callback(err,data), but use them in sequential/SYNC mode when required.
- Plays along with node cluster. You design for one thread/processor, then scale with cluster on multicores.
With npm installed, run
$ npm install --save fiberit
import { Fiberit } from 'fiberit';
const someAsyncFunction = (param: number, cb: NodeCallback<number>) => setTimeout(() => cb(null, param * 2), 100);
Fiberit.launchFiber(() => {
const result = Fiberit.for(someAsyncFunction, 5);
console.log(result); // 10
})
Calls the function inside a fiber.
Required
Type: Function
Function to run inside the fiber
Calls the function inside a fiber.
Required
Type: Function
Async function with node-style callback
Type: any
N parameters that will be passed to the asyncFunction
Calls the function inside a fiber.
Required
Type: Object
Object that has the method that will be called inside the fiber
Required
Type: String
Method of the object with node-style callback
Type: any
N parameters that will be passed to the object.method
Calls the function inside a fiber.
Required
Type: Object
Object that has the method that will be called inside the fiber
Required
Type: String
Method of the object which return a Promise
Type: any
N parameters that will be passed to the object.method