[Pkg-javascript-commits] [node-pend] 01/10: initial version

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Fri Jun 27 22:11:00 UTC 2014


This is an automated email from the git hooks/post-receive script.

andrewrk-guest pushed a commit to branch master
in repository node-pend.

commit 76c8c938d5cb329b165f98d729917df7679b4d5d
Author: Andrew Kelley <superjoe30 at gmail.com>
Date:   Fri Aug 2 02:40:47 2013 -0400

    initial version
---
 README.md    | 41 +++++++++++++++++++++++++++++++++++++++++
 index.js     | 37 +++++++++++++++++++++++++++++++++++++
 package.json | 11 +++++++++++
 test.js      | 22 ++++++++++++++++++++++
 4 files changed, 111 insertions(+)

diff --git a/README.md b/README.md
new file mode 100644
index 0000000..bf04a5c
--- /dev/null
+++ b/README.md
@@ -0,0 +1,41 @@
+# Pend
+
+Dead-simple optimistic async helper.
+
+## Usage
+
+```js
+var Pend = require('pend');
+var pend = new Pend();
+pend.go(function(cb) {
+  console.log("this function is immediately executed");
+  setTimeout(function() {
+    console.log("calling cb 1");
+    cb();
+  }, 500);
+});
+pend.go(function(cb) {
+  console.log("this function is also immediately executed");
+  setTimeout(function() {
+    console.log("calling cb 2");
+    cb();
+  }, 1000);
+});
+pend.wait(function(err) {
+  console.log("this is excuted when the first 2 have returned.");
+  console.log("err is a possible error in the standard callback style.");
+});
+```
+
+Output:
+
+```
+this function is immediately executed
+this function is also immediately executed
+calling cb 1
+calling cb 2
+this is excuted when the first 2 have returned.
+err is a possible error in the standard callback style.
+```
+
+This readme file is longer than the source code.
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..954edf2
--- /dev/null
+++ b/index.js
@@ -0,0 +1,37 @@
+module.exports = Pend;
+
+function Pend() {
+  this.pending = 0;
+  this.listeners = [];
+  this.error = null;
+}
+
+Pend.prototype.go = function(fn) {
+  pendGo(this, fn);
+};
+
+Pend.prototype.wait = function(cb) {
+  if (this.pending === 0) {
+    cb(this.error);
+  } else {
+    this.listeners.push(cb);
+  }
+};
+
+function pendGo(self, fn) {
+  self.pending += 1;
+  fn(onCb);
+  function onCb(err) {
+    self.error = self.error || err;
+    self.pending -= 1;
+    if (self.pending < 0) throw new Error("Callback called twice.");
+    if (self.pending === 0) {
+      self.listeners.forEach(cbListener);
+      self.listeners = [];
+    }
+  }
+  function cbListener(listener) {
+    listener(self.error);
+  }
+}
+
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..c1e5021
--- /dev/null
+++ b/package.json
@@ -0,0 +1,11 @@
+{
+  "name": "pend",
+  "version": "1.0.0",
+  "description": "dead-simple optimistic async helper",
+  "main": "index.js",
+  "scripts": {
+    "test": "node test.js"
+  },
+  "author": "Andrew Kelley <superjoe30 at gmail.com>",
+  "license": "MIT"
+}
diff --git a/test.js b/test.js
new file mode 100644
index 0000000..c676858
--- /dev/null
+++ b/test.js
@@ -0,0 +1,22 @@
+var assert = require('assert');
+var Pend = require('./');
+var pend = new Pend();
+var results = [];
+pend.go(function(cb) {
+  results.push(1);
+  setTimeout(function() {
+    results.push(3);
+    cb();
+  }, 500);
+});
+pend.go(function(cb) {
+  results.push(2);
+  setTimeout(function() {
+    results.push(4);
+    cb();
+  }, 1000);
+});
+pend.wait(function(err) {
+  assert.deepEqual(results, [1,2,3,4]);
+});
+assert.deepEqual(results, [1, 2]);

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-pend.git



More information about the Pkg-javascript-commits mailing list