New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

sync

Package Overview
Dependencies
Maintainers
0
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sync

Library that makes simple to run asynchronous functions in synchronous manner, using node-fibers.

  • 0.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
888
decreased by-30.13%
Maintainers
0
Weekly downloads
 
Created
Source

Introduction

node-sync is a simple library that allows you to call any asynchronous function in synchronous way. The main benefit is that it uses javascript-native design - Function.prototype.sync function, instead of heavy APIs which you'll need to learn. Also, asynchronous function which was called synchronously through node-sync doesn't blocks the whole process - it blocks only current thread!

It built on node-fibers library as a multithreading solution.

You may also like fibers-promise and node-fiberize libraries.

Examples

Simply call asynchronous function synchronously: function asyncFunction(a, b, callback) { process.nextTick(function(){ callback(null, a + b); }) }

// Function.prototype.sync() interface is same as Function.prototype.call() - first argument is 'this' context
var result = asyncFunction.sync(null, 2, 3);
console.log(result); // 5

// Read file synchronously without blocking whole process? no problem
var source = require('fs').readFile.sync(null, __filename);
console.log(String(source)); // prints the source of this example itself

It throws exceptions! function asyncFunction(a, b, callback) { process.nextTick(function(){ callback('something went wrong'); }) }

try {
	var result = asyncFunction.sync(null, 2, 3);
}
catch (e) {
	console.error(e); // something went wrong
}

Parallel execution: var Sync = require('sync');

// Parallel function will return values only when all callbacks will be executed
var result = Sync.Parallel(function(callback){
	asyncFunction(2, 3, callback());
	asyncFunction(5, 5, callback());
	asyncFunction(10, 10, callback());
});
console.log(result); // [5, 10, 20]

// Associative result
var result = Sync.Parallel(function(callback){
	asyncFunction(2, 3, callback('foo'));
	asyncFunction(5, 5, callback('bar'));
	asyncFunction(10, 10, callback('baz'));
});
console.log(result); // { foo: 5, bar: 10, baz: 20 }

See more examples in examples directory.

Installation

install npm install sync and then node-fibers your_file.js

FAQs

Package last updated on 11 Mar 2011

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc