Created
June 15, 2012 19:28
-
-
Save pulkitsinghal/2938316 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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); | |
| } | |
| } | |
| ); | |
| } |
This file contains hidden or 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
| async.waterfall( | |
| [ | |
| getUUID (callback), | |
| createUser(uuid, callback) | |
| ], | |
| function (err, result) { | |
| console.log(result); | |
| } | |
| ); |
This file contains hidden or 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
| 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'); | |
| } | |
| } | |
| ); | |
| } |
This file contains hidden or 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
| async.waterfall( | |
| [ | |
| function (callback) { | |
| getUUID (callback); | |
| }, | |
| function (uuid, 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
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);
}
);