JavaScript - Implementing Safe Assignment Without new Operator

First of all, I appreciate the effort and thought that has gone into the proposal for the ?= operator. It's clear that the intention is to simplify and standardize error handling in JavaScript, which is an important goal.

However, I believe that this proposal might not be necessary given that we can achieve similar results using existing JavaScript features. Specifically, we can handle errors and return results in a concise and readable way by using simple wrapper functions. Here's an example of how this can be done:

const synco = (operation) => {
   let result = null;
   let error = null;

   try {
      result = operation(); 
   } catch(err) {
      error = err;
   }

   return [error, result];
};

const asynco = async (operation) => {
   let result = null;
   let error = null;

   try {
      result = await operation();
   } catch(err) {
      error = err;
   }

   return [error, result];
};

Moreover, if we use our imagination, we can extend these functions to return not just tuples, but also objects or any other robust structures that might suit the specific needs of a given application. This flexibility allows developers to tailor their error handling to the exact requirements of their projects, without being constrained to a single approach. For example, we could return an object with additional metadata, logs, or context about the error and the operation, providing a more comprehensive solution.

Sync example

// Operation to be executed synchronously
const parseJson = () => {
   const data = JSON.parse('{"key": "value"}'); // Example of a potentially error-prone operation
   return data.key;
};

// Execute the operation using synco
const [error, result] = synco(parseJson);

if (error) {
   console.error('An error occurred:', error.message);
} else {
   console.log('Result:', result); // Output: "Result: value"
}

Async example

// Operation to be executed asynchronously
const fetchData = async () => {
   const response = await fetch('https://api.example.com/data');
   const data = await response.json();
   return data.key;
};

// Execute the operation using asynco
const [error, result] = await asynco(fetchData);

if (error) {
   console.error('An error occurred:', error.message);
} else {
   console.log('Result:', result); // Output: the value of data.key if successful
}

Key Points:

  • Simplicity: These functions encapsulate the try-catch logic, returning an error-result tuple similar to what the ?= operator aims to achieve.
  • Compatibility: Both sync and async operations are handled in a clean and straightforward manner.
  • No Language Changes Needed: This approach doesn't require any changes to the JavaScript language, and it can be easily implemented today without waiting for language updates.

Conclusion:

While the ?= operator is an interesting idea, it's important to consider whether introducing a new operator is justified when we can already accomplish the same goals with existing language features. The above functions provide a simple, readable, and effective way to handle errors without adding complexity to the language.

I'd love to hear thoughts from the community on this approach and whether it might be a sufficient alternative to the proposed operator.

Thanks for considering my input!