I'm trying to understand Promises. I've create some promise chains that work and others that don't. I've made progress but am apparently lacking a basic concept. For example, the following promise chain doesn't work. It's a silly example, but shows the issue; I'm trying to use Node's function randomBytes twice in a chain:
var Promise = require("bluebird");
var randomBytes = Promise.promisify(require("crypto").randomBytes);
randomBytes(32)
.then(function(bytes) {
if (bytes.toString('base64').charAt(0)=== 'F') {
return 64; //if starts with F we want a 64 byte random next time
} else {
return 32;
}
})
.then(randomBytes(input))
.then(function(newbytes) {console.log('newbytes: ' + newbytes.toString('base64'));})
The error that arrises here is "input
is undefined." Am I trying to do something that can't (or shouldn't) be done?