Skip to content

jDevxyz/synchronous-ify

Repository files navigation

Synchronous-ify (Looking for a contributor!)

A minimum package to run asynchronous job synchronously

Standard - JavaScript Style Guide Join Discord server

About

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.

Installation

npm install --save github:jDevxyz/synchronous-ify

Compatibility

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.

Examples

Simple

This covers how to start a Pipestream session

const { Pipe, Strandpipe } = require('synchronous-ify')
const stream = new Strandpipe()
Pipe(function() {
  do this
}).run()
...

HTTP Request

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()
...

Database

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()
...

Runner (Alpha)

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)
})
...

Listener (Alpha)

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.

Why Synchronous-ify?

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.

Synchronous-ify in action

Here some modules that use Synchronous-ify as their dependency.

License

synchronous-ify is licensed under the GNU AGPL-3.0 © J-Dev

Contribution

Standard - JavaScript Style Guide

Contribution is much appreciated. Do a pull request once you're done with your changes. Don't forget to put specifically which part you commited, and why. Open issue when you find something wrong with our package.