I'm not quite sure, and maybe I am missing something obvious, but I cannot figure out how to link the two promises.
My callback based code looks something like this:
async.series([
function (cb) {
if (!nodir) {
fs.mkdir('somedirectory', function (err) {
if (err) {
log('error while trying to create the directory:\n\t %s', err)
process.exit(0)
}
log('successfully created directory at %s/somedirectory', process.cwd())
cb(null)
})
}
cb(null)
},
function (cb) {
fs.readFile(path.dirname(__dirname) + '/util/sample_config.yml', function (err, c) {
var sampleContent = c
if (err) {
log('error while reading the sample file:\n\t %s', err)
process.exit(0)
}
log('pulled sample content...')
fs.writeFile('db/config.yml', sampleContent, function (err) {
if (err) {
log('error writing config file:\n\t %s', err)
process.exit(0)
}
log('successfully wrote file to %s/db/config.yml', process.cwd())
cb(null)
})
})
}
])
I started trying to reorganize this into a promise-based thread, this is what I still have:
if (!nodir) {
fs.mkdirAsync('somedirectory').then(function () {
log('successfully created directory at %s/somedirectory', process.cwd())
}).then(function () {
}).catch(function (err) {
log('error while trying to create the directory:\n\t %s', err)
process.exit(0)
})
}
(I use Bluebird, so I did Promise.promisifyAll(fs)before)
The problem is that I do not know where to put the second "step" of the previous series. Do I put it in thenor in its function? Do I return it and put it in a separate function?
Any help would be greatly appreciated.
jona source
share