npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sync-es6

v1.1.0

Published

How to write in sync (with only generator ES6), based on waitfor-ES6

Downloads

51

Readme

About

The most native and simplest way to write-in-sync in NodeJS

This library is written purely in ES6 without any dependency

Still non-blocking node's event loop

Fully compatible with all other libraries (such as async)

NEW UPDATE: support Promise call (BETA - document pending) Example

Install

  • npm i sync-es6 --save

Usage

CALLBACK HELL:

main(callback) {
	async_function(arg1, (err, result1) => {
		another_async_function(arg2, (err, result2) => {
			return callback && callback(null, result1 + result2);
		});
	});
}

Step 0: make sure all of your callback functions will be callbacked as "function (err, result) {}" format

  • 1st param: error indicated (usually null)
  • 2nd param: real return value

Step 1: define wraper function as a generator function, and call inside functions in sync

function* writeSyncInside(args1, args2) { 
	let returnValue1 = yield [async_function, arg1];
	let returnValue2 = yield [another_async_function, arg2];

	return returnValue1 + returnValue2;
}

Step 2: CALL IT with sync

const _sync = require('sync-es6');

_sync(writeSyncInside /*,args1, args2, ... , callback*/);
// optional callback function at last argument, nice to have in case of error occurred

Sample code:

Syntax:

let iterator = _sync(runner,args1, args2, ... , finalcallback);
  • Parameters

    • runner (required): a generator function OR object {generator: function*, isTolerant: boolean}
      • isTolerant (optional, default "false"): if it's "true", when an error occurred in inside async function (when callback return (err)) the runner will not throw & break to finalcallback (if existed) and just return undefined
    • args (optional): input params for generator function
    • finalcallback (optional): catch error & final result for runner, if it's undefined the runner will throw() when error occurred
  • Return

    • iterator (optional): IT'S NOT final result value, just a iterator. Normally you wouldn't use it. It has .break(err) method when you want to break from runner function anytime.
iterator.break(err) // break exit from inside runner (generator) function.
  • Parameters
    • err (required): Error/String/... anything that's not null

Credit:

I write this library in thanks of Mr.Luciotato that looking into his code help me learn about generator function. I guess that because of he want the wait.for-es6 should be backward compatible with wait.for (ES5) so there something messy if you wall to call multiple async functions inside each others (with the "onComplete" event) OR maybe just I don't know how to use it properly. So I decide to seperate with a little modification to make it easier to use.