0.9.1 • Published 10 years ago

resync v0.9.1

Weekly downloads
69
License
-
Repository
github
Last release
10 years ago

Resync

Build Status Coverage Status

Features

  • Node callback management like synchronous code
  • Promise support for
    • Native ES6 promises
    • Bluebird

Usage

Installation

Install this package using npm:

npm install --save resync

Examples

Database query

var Resync = require('resync');
var Pg = require('postgres');

var loadProducts = Resync(function * (wait) {
  var pg = yield Pg.connect('postgres://username:password@localhost/database', wait());
  var client = pg[0];
  var release = pg[1];

  var result = yield client.query('SELECT * FROM product', wait());

  return result.rows;
});

loadProducts(function (err, products) {
  // Handle those errors & do something with your products
});

Multiple results

var Resync = require('resync');
var Redis = require('redis');

var loadCachedProducts = Resync(function * (ids, wait) {
  var redis = Redis.createClient();

  for (let id of ids) {
    redis.hgetall(`products:${id}`, wait());
  }

  return yield Array;
});

Passing parameters

var Resync = require('resync');
var Pg = require('postgres');

var loadProduct = Resync(function * (id, wait) {
  var pg = yield Pg.connect('postgres://username:password@localhost/database', wait());
  var client = pg[0];
  var release = pg[1];

  var result = yield client.query('SELECT * FROM product WHERE product = ?', [id], wait());

  return result.rows;
});

loadProduct(111, function (err, product) {
  // Handle those errors & do something with your product
});

Handling errors

var Resync = require('resync');
var Fs = require('fs');

var loadProduct = Resync(function * (directory, wait) {
  try {
    return yield Fs.readdir(directory, wait());
  } catch (err) {
    return [];
  }
});

loadProduct('/var/awesome/dir', function (err, files) {
  // Handle those errors or do something with your files
});
0.9.1

10 years ago

0.9.0

10 years ago

0.8.1

10 years ago

0.8.0

10 years ago

0.7.0

10 years ago

0.6.0

10 years ago

0.5.0

10 years ago

0.4.0

10 years ago

0.3.1

10 years ago

0.3.0

10 years ago

0.2.0

10 years ago

0.1.0

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago