Created
May 15, 2012 13:10
-
-
Save totallymike/2701630 to your computer and use it in GitHub Desktop.
setInterval closure trick that's not sustainable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var assert = require('assert') | |
, cradle = require('cradle') | |
, conn = new(cradle.Connection)('https://myCouch.couch',443,{ | |
auth: {username: "mike", password: "pass"}}) | |
, db = conn.database('articles'); | |
function saveDoc(doc) { | |
db.save(doc.data, printResult); | |
} | |
function printResult(err, response) { | |
if (err) { | |
console.log('Error: ' + err.error); | |
console.log('Reason: ' + err.reason); | |
} else | |
console.dir(response); | |
} | |
var documents = [ | |
{ | |
func : saveDoc, | |
data: { | |
type:'post', | |
html:'Test 2' | |
} | |
}, | |
{ | |
func : saveDoc, | |
data: { | |
type: 'post', | |
html: 'Test 1', | |
author: 'Michael Westbom' | |
} | |
}, | |
{ | |
func : function(doc) { | |
conn.uuids(2, function(err,res) { | |
res.forEach(function(id) { console.log('id: ' + id); }); | |
for (var i = 0; i < 2; i += 1) { | |
db.save(res[i], doc.data[i], function(e, r) { | |
if (e) { | |
console.log('Save with ID error: ' + e.reason); | |
} else { | |
console.log('Save with ID result: '); | |
console.dir(r); | |
} | |
}); | |
} | |
}); | |
}, | |
data: [ | |
{ | |
type: 'post', | |
html: 'Test HTML 2', | |
created_at : new Date().toISOString(), | |
author: 'Michael Westbom', | |
title: 'Test 2' | |
}, | |
{ | |
type : 'post', | |
html: 'Test HTML 3', | |
created_at: new Date().toISOString(), | |
author: 'Michael Westbom', | |
title: 'Test 3' | |
} | |
] | |
}, | |
{ | |
func : function(doc) { | |
db.view('articles/all', function(err, res) { | |
if (err) console.dir(err); | |
else console.dir(res); | |
/*res.forEach(function (row) { | |
console.log("title: %s\nhtml: %s", row.title, row.html); | |
});*/ | |
}); | |
}, | |
}, | |
]; | |
setInterval( (function(docs) { | |
return function() { | |
var doc = docs.shift(); | |
doc.func(doc); | |
if (docs.length < 1) | |
clearInterval(this); | |
} | |
})(documents), 1000); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment