skip to Main Content

I’ve been going through Medium to get some updates about programming. A couple of articles confused me that were related to "Safe Assignment Operator":

async function getData() {
    const [networkError, res] ?= await fetch('https://api.backend.com/resource/1')
    
    if(networkError) console.error(networkError)
    const [parseError, data] ?= await res.json()

    if(parseError) console.error(parseError)
    return data
            
}

I tried finding the official documentation on MDN, but nothing found. I also tried on console but no luck.

Is this a myth or isn’t the MDN site updated?

Here is the article that build up the confusion. Medium Article

1

Answers


  1. There is a proposal for a safe assignment operator (?=). The aim is to simplify error handling without needing try/catch but using:

    const [error, result] ?= doWork();
    

    as opposed to:

    try {
      const result = doWork();
    } catch (error) {
    
    }
    

    However, this is still a very early proposal that does not even show up on the proposal track for ECMAScript. This is typically a six stage process with stage zero being just marked as there but not yet considered. The proposal is not at stage zero yet.

    Moreover, the earliness of the proposal is evident by looking at its README which currently says on top:

    Caution

    This proposal will change to try-expressions as its a more idiomatic apporach to this problem. Read more on #4 and #5.

    Help on its rewriting is needed 🙂

    So, it can change further before even entering the proposal track from TC39.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search