Why won't this Node snippet catch the rejected promise?

I am trying to understand why the Promise exception I am facing is not being handled by my initial try / trick in the following code (full code is in this thread )

This particular error nodeCf.deploy

is called in index.js.

The error is happening because I have to use this.deployName

, not stack.deployName

in my console.log statement.

What I don't understand is why this is considered an undeclared promise. Should the try / catch attempt on the first call to nodeCf.deploy catch this?

// index.js:

  switch (args.action) {
    case 'deploy':
      try {
        await nodeCf.deploy(stacks, envVars);
      } catch (e) {
        console.log(`deployment failed: `, e);
        process.exit(1);
      }
      break;
  < ... >

// nodeCf module:
async deploy(stacks, envVars) {
  var stackOutputs = {};
  await Promise.each(stacks, async(stack) => {
    stackOutputs[stack.name] = await stack.deploy(envVars, 
      stackOutputs).outputs;
  });
}

// stack.deploy:
async deploy(envVars, stackOutputs) {
this.load(envVars, stackOutputs);
await ensureBucket(this.infraBucket);
const s3Resp = await this.uploadTemplate()
const stackResp = await ensureAwsCfStack({
  StackName: this.deployName,
  Parameters: this.parameters,
  Tags: this.tags,
  TemplateURL: s3Resp.Location,
  Capabilities: [ 'CAPABILITY_IAM', 
    'CAPABILITY_NAMED_IAM' ]
});
this.outputs = _.map(stackResp.Outputs, (it) => 
  _(it).pick(['OutputKey', 'OutputValue'])
    .toPairs()
    .unzip()
    .tail()
    .fromPairs()
    .value());
console.log(`deployed ${stack.deployName}`); // 'stack' is causing exception
return this;
}

      

+3


source to share


1 answer


Here is your problem:

stackOutputs[stack.name] = await stack.deploy(envVars, stackOutputs).outputs;

      

Notice what stack.deploy

is async function

? This means it returns a promise - not an instance with a property outputs

. You get access to .outputs

on-promise, which undefined

will be await

ed just fine, but the promise and its rejection are ignored.



You need to write

stackOutputs[stack.name] = (await stack.deploy(envVars, stackOutputs)).outputs;
//                         ^                                         ^

      

or use a helper variable for the instance.

+8


source







All Articles