Javascript set const variable inside of a try block

Javascriptnode.jsEcmascript 6Try Catch

Javascript Problem Overview


Is it possible in ES6 to set a variable inside of a try{} using const in strict mode?

'use strict';

const path = require('path');

try {
	const configPath = path.resolve(process.cwd(), config);
} catch(error) {
	//.....
}

console.log(configPath);

This fails to lint because configPath is defined out of scope. The only way this seems to work is by doing:

'use strict';

const path = require('path');

let configPath;
try {
	configPath = path.resolve(process.cwd(), config);
} catch(error) {
	//.....   
}

console.log(configPath);

Basically, is there any way to use const instead of let for this case?

Javascript Solutions


Solution 1 - Javascript

Declaring a variable as const requires you to immediately point it to a value and this reference cannot be changed.

Meaning you cannot define it at one place (outside of try) and assign it a value somewhere else (inside of try).

const test; // Syntax Error
try {
  test = 5; 
} catch(err) {}

On the other hand, both creating it and giving it a value within the try block is fine.

try {
  const test = 5; // this is fine
} catch(err) {}

However, const is block-scoped, like let, so if you do create it and give it a value within your try block, it will only exist within that scope.

try {
  const test = 5; // this is fine
} catch(err) {}
console.log(test); // test doesn't exist here

Therefore, if you need to access this variable outside of the try, you must use let:

let configPath;
try {
   configPath = path.resolve(process.cwd(), config);
} catch(error) {
    //.....   
}

console.log(configPath);

Alternatively, although probably more confusingly, you can use var to create a variable within the try and use it outside of it because var is scoped within the function, not the block (and gets hoisted):

try {
   var configPath = path.resolve(process.cwd(), config);
} catch(error) {
    //.....   
}

console.log(configPath);

Solution 2 - Javascript

'use strict';

const path = require('path');

const configPath = (function() {
  try {
    return path.resolve(process.cwd(), config);
  } catch (error) {
    //.....
  }
})()

console.log(configPath);

Solution 3 - Javascript

I would try to use a temp variable with let and assign that to a const var after the try/catch and 'delete' the temp var.

'use strict';

let temp;
try {
  temp = path.resolve(process.cwd(), config);
} catch (error) {
  //.....   
}

const configPath = temp;
temp = undefined;

console.log(configPath);

Solution 4 - Javascript

You can avoid the try block altogether if the function is async! Just learned this today, thought I'd share!

More broadly applicable to just your situation as this is the top Google result for "const in a try block"

'use strict';
const path = require('path');

const getPath = async () => {
    return path.resolve(process.cwd())
}

const logPath = async () => {
    const configPath = await getPath().catch(e => console.log(e)) <--avoid the try
    console.log(configPath);
}

logPath()

Works great when you're already in an async function and need to wait for something else:

async function main() {
  const result = await asyncTask().catch(error => console.error(error));
}

Learned from: https://stackoverflow.com/a/55024396/2936521

Solution 5 - Javascript

There are plenty of good answers here. But it's real annoying to have to manage your lets being inside and out of scope. So if you are like me, and came here searching for a better pattern. I wrote a little utility that helps a ton.

export const tc = <T>(option: { try: () => T; catch: (e: Error) => T }) => {
  try {
    return option.try()
  } catch (e) {
    return option.catch(e)
  }
}

Here are some unit tests to show how it's useful

import { tc } from './tc'
describe('tc', () => {
  it('should return successfully', () => {
    const result = tc({
      try: () => 1 + 2,
      catch: () => -1,
    })
    expect(result).toEqual(3)
  })
  it('should catch', () => {
    const spy = jest.fn()
    const result = tc({
      try: (): number => {
        throw new Error('Test')
      },
      catch: (e) => {
        spy()
        expect(e.message).toEqual('Test')
        return -1
      },
    })
    expect(spy).toHaveBeenCalledTimes(1)
    expect(result)
  })
  it('should rethrow', () => {
    expect(() =>
      tc({
        try: (): number => {
          throw new Error('Test')
        },
        catch: (e) => {
          throw e
        },
      }),
    ).toThrowError()
  })
  it('should have proper types', () => {
    // @ts-expect-error
    const result: string = tc({
      try: () => 12,
      catch: (e) => {
        return -1
      },
    })
  })
})

Solution 6 - Javascript

Besides the let options I see here, another option may be to use an object, as the reference to the object is constant, but it's properties can be altered, so something like this:

'use strict';

const path = require('path');

const configPath = { value: null };

try {
    configPath.value = path.resolve(process.cwd(), config);
} catch(error) {
    //.....
}

console.log(configPath.value);

It would probably be cleaner to stick with let, but I just wanted to point out another possible option.

Solution 7 - Javascript

Use let. You cannot use const. const does not allow you to reassign the declared constant. While it is generally good practice to declare objects like yours with const, the entire point of doing so is to allow objects to be mutated without allowing them to be reassigned. You are reassigning the object (thus, defeating the purpose of const), so use let instead.

let path = require('path');
// Good to go!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJustinView Question on Stackoverflow
Solution 1 - Javascriptnem035View Answer on Stackoverflow
Solution 2 - Javascriptsteph643View Answer on Stackoverflow
Solution 3 - JavascriptyunzenView Answer on Stackoverflow
Solution 4 - JavascriptAlteredorangeView Answer on Stackoverflow
Solution 5 - JavascriptEric WooleyView Answer on Stackoverflow
Solution 6 - JavascriptZer0View Answer on Stackoverflow
Solution 7 - JavascriptKyle LinView Answer on Stackoverflow