Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

simpleasync

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simpleasync - npm Package Compare versions

Comparing version 0.0.1-alpha to 0.0.1

sample1.js

29

lib/simpleasync.js

@@ -1,1 +0,28 @@

function Sequence() { } function createSequence() { return new Sequence(); } module.exports = createSequence;
function Runner(fns, elsefn) { var k = 0; var l = fns.length; var data = null; this.run = function (initialdata) { data = initialdata; doStep(); } function doStep() { if (k >= l) return; var fn = fns[k++]; try { if (fn.length > 1) { fn(data, function (err, newdata) { if (err) { elsefn(err); return; } data = newdata; setImmediate(doStep); }); } else { data = fn(data); setImmediate(doStep); } } catch (err) { elsefn(err); return; } } }
function Sequence() {
var fns = [];
var elsefn = function (err) { throw err;
}
this.then = function (fn) {
fns.push(fn);
return this;
}
this.else = function (fn) {
elsefn = fn;
return this;
}
this.run = function (data) { var runner = new Runner(fns, elsefn); setImmediate(runner.run(data));
return this;
}
}
function createSequence() {
return new Sequence();
}
module.exports = createSequence;

2

package.json
{ "name": "simpleasync"
, "description": "Simple flow library to chain and run functions with asynchronous calls"
, "keywords": [ "node", "asynchronous", "library" ]
, "version": "0.0.1alpha"
, "version": "0.0.1"
, "author": "Angel 'Java' Lopez <webmaster@ajlopez.com> (http://www.ajlopez.com)"

@@ -6,0 +6,0 @@ , "repository": { "type": "git", "url": "git://github.com/ajlopez/SimpleAsync.git" }

@@ -20,4 +20,79 @@ # SimpleAsync

```
[TBD] (see tests).
Define and run steps with then:
```js
async()
.then(function (data) {
console.log(data);
return data + 1;
})
.then(function (data) {
console.log(data);
})
.run(10);
```
Expected output
```
10
11
```
Define and run steps with one step using a callback. Then, you can write async steps:
```js
async()
.then(function (data, next) {
console.log(data);
next(null, data + 1); // callback(err, newdata);
})
.then(function (data) {
console.log(data);
})
.run(10);
```
Expected output
```
10
11
```
Use else for catch errors:
```js
async()
.then(function (data) {
console.log(data);
throw "Houston, we have a problem";
})
.else(function (err) {
console.log('error:', err);
})
.run(10);
```
Expected output
```
10
error: Houston, we have a problem
```
Raise an error in an step with callback
```js
async()
.then(function (data, next) {
console.log(data);
next("Houston, we have a problem", null);
})
.else(function (err) {
console.log('error:', err);
})
.run(10);
```
Expected output
```
10
error: Houston, we have a problem
```
## Development

@@ -24,0 +99,0 @@

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