Skip to content

Instantly share code, notes, and snippets.

@pulkitsinghal
Created June 15, 2012 19:28
Show Gist options
  • Select an option

  • Save pulkitsinghal/2938316 to your computer and use it in GitHub Desktop.

Select an option

Save pulkitsinghal/2938316 to your computer and use it in GitHub Desktop.
function getUUID() {
nano.request(
{
method : 'GET',
path : '_uuids?count=1'
},
function (err, body, headers) {
if (err) {
console.error(err);
} else {
console.log(body.uuids[0]);
}
}
);
}
function createUser(uuid) {
var usersDB = nano.db.use('_users');
usersDB.insert(
{
'name' : uuid,
'password' : 'pwdUser1',
'roles' : [],
'type' : 'user'
},
'org.couchdb.user:'+ uuid,
function(err, body, header) {
if (err) {
console.error('[_users.insert] ', err);
} else {
console.log('you have inserted the uuid.')
console.log(body);
}
}
);
}
async.waterfall(
[
getUUID (callback),
createUser(uuid, callback)
],
function (err, result) {
console.log(result);
}
);
function getUUID(callback) {
nano.request(
{
method : 'GET',
path : '_uuids?count=1'
},
function (err, body, headers) {
if (err) {
console.error(err);
} else {
console.log(body.uuids[0]);
callback(null, body.uuids[0]);
}
}
);
}
function createUser(uuid, callback) {
var usersDB = nano.db.use('_users');
usersDB.insert(
{
'name' : uuid,
'password' : 'pwdUser1',
'roles' : [],
'type' : 'user'
},
'org.couchdb.user:'+ uuid,
function(err, body, header) {
if (err) {
console.error('[_users.insert] ', err);
} else {
console.log('you have inserted the uuid.')
console.log(body);
callback(null, 'done');
}
}
);
}
async.waterfall(
[
function (callback) {
getUUID (callback);
},
function (uuid, callback) {
createUser(uuid, callback);
}
],
function (err, result) {
console.log(result);
}
);
@fsrc
Copy link
Copy Markdown

fsrc commented Oct 12, 2012

Or you could just pass the functions as variables..

// Works
async.waterfall([
getUUID,
createUser
],
function (err, result) {
console.log(result);
}
);

Error in your first thought solution is that the functions get called in place since you pass params to them.

// Does not work
async.waterfall(
[
getUUID (callback),
createUser(uuid, callback)
],
function (err, result) {
console.log(result);
}
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment