I use the Inquirer library with Node.js and I still get the doom pyramid when using promises, what am I doing wrong?
Just FYI request library API basically:
inquirer.prompt([ question1, question2, question3, ... questionX ]).then(function(answers){});
where the answers are a hash, with keys that represent each question. There is nothing unusual here.
In any case, using the API, I always get getAnswersToPrompts().then(function(answers){})
, and it seems more convenient to keep the nested promises inside the previous one ... like this:
function run (rootDir) { return watchHelper().then(function (answers) { return chooseDirs({ allowDirs: answers.allow, originalRootDir: rootDir, onlyOneFile: false }).then(function (pathsToRun) { assert(pathsToRun.length > 0, ' You need to select at least one path.'); return getOptions(availableOptionsForPlainNode).then(function (answers) { const selectedOpts = answers[ 'command-line-options' ]; return localOrGlobal().then(function (answers) { const sumanExec = answers.localOrGlobal; console.log(' => ', colors.magenta.bold([ '$', sumanExec, '--watch', pathsToRun, selectedOpts ].join(' '))); }); }); }); }).catch(rejectionHandler); }
I could do this instead:
function run(){ return makePromise() .then(fn1(data1)) .then(fn2(data2)) .then(fn3(data3)) }
where fn1, fn2, fn3 are as follows:
function fnX(data){ return function(answers){ return promise(data); } }
but it only complicates the understanding of AFAICT
Just be extremely clear, I definitely need the result of the previous promise, but sometimes I need the result from the promise to this or even the result to that.
Nesting functions allows me to get the data I need using closures, etc.