GithubHelp home page GithubHelp logo

thatismunn / synchronous-ify Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jdevxyz/synchronous-ify

0.0 0.0 0.0 149 KB

A minimum package to run asynchronous job synchronously

License: GNU Affero General Public License v3.0

JavaScript 100.00%

synchronous-ify's Introduction

Synchronous-ify

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.

Contribution

Standard - JavaScript Style Guide

Synchronous-ify using GNU AFFERO General Public License, and whoever obtains the code also gain access to every corner of this module. Contribution is much appreciated. Do a pull request once you're done with your changes.

synchronous-ify's People

Contributors

billyaddlers avatar hazmi35 avatar thatismunn avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.