A minimum package to run asynchronous job synchronously
Synchronous-ify is a lightweight simple package consist of function wrappers and resolver for Async/Await and Promises. It is built above the blazingly fast Coroutines Thread module, node-fibers, to provide exposed API and make synchronous code runs with ease inside an asynchronous environment.
This Module is a SNAPSHOT, hence bugs are expected. Contribute to us via Pull Request.
npm install --save github:jDevxyz/synchronous-ify
Synchronous-ify relies a lot on node-fibers, hence the compatibility depends on whether node-fibers supports it or not. Check their list of supported platforms for more info.
This covers how to start a Pipestream session
const { Pipe, Strandpipe } = require('synchronous-ify')
const stream = new Strandpipe()
Pipe(function() {
do this
}).run()
...
You can quickly resolve a result from HTTP GET request.
...
const fetch = require('node-fetch')
Pipe(function() {
const result = stream.sync(stream.sync(fetch('https://httpbin.org/get').json())) // Obtains the JSON result
console.log(result)
}).run()
...
Or simply use it to obtains data from database
...
const mysql = require('mysql')
const connection = mysql.createConnection(...blablabla)
Pipe(function() {
// If any error happened, it will be thrown automatically
const query = stream.sync(connection.query(`SELECT * FROM levels WHERE userId = '${user.uuid}'`))
console.log(query)
}).run()
...
You can also use runner to quickly jump-and-use Synchronous-ify API.
The runner will returns an instance of Strandpipe
as callback. This way, you don't have to construct the stream by yourself.
const { Threadify } = require('synchronous-ify')
Threadify.runner((stream) => {
const fetch = require('node-fetch')
const result = stream.sync(stream.sync(fetch('https://httpbin.org/get').json()))
console.log(result)
})
...
Need the value outside of Runner? No worries. Listener will handle that.
const { PipeListener, Threadify } = require('synchronous-ify')
const listener = new PipeListener()
Threadify.runner((stream) => {
const fetch = require('node-fetch')
const result = stream.sync(stream.sync(fetch('https://httpbin.org/get').json()))
listener.listen(result)
})
listener.on('resolve:result', (res) => {
console.log(res) // you can access the result of node-fetch in here
})
...
WARNING! This example is written without consederation and lack of sleep. Mistakes may exist, and wrong use of library function are possible. Documentation is generated by jsdoc-to-markdown.
There's a lots of module wrapper for node-fibers, but why Synchronous-ify? Synchronous-ify is actively developed by a lots of people on the Community. Unlike others who dominantly developed in the ancient days of old NodeJS, Synchronous-ify is using latest compatibility of NodeJS 10. Furthermore, this module supports TypeScript as well.
Here some modules that use Synchronous-ify as their dependency.
- node-fiberfetch - node-fetch wrapper, using Synchronous-ify style.