Skip to content

Since use of 'ForOfStatement' is not allowed ... #1931

@hvkale

Description

@hvkale

Pretty sure this has been discussed thousands of times but haven't found a perfect replacement for my code snippet below.

Since the use of for...of construct is not allowed, how can I write following code?

// lines is an array of strings
let value = ''
for (const line of lines) {
   try {
      const json = JSON.parse(line)
      if (json) {
        value = json.whatever 
        /* 
           more logic irrelevant to this question
        */
        // As soon as first jsonable line is found and processed, short-circuit the loop
        break
      }
    } catch (err) {
      // Handle error
      // throw / reject / console.error() / return - could be anything
    }  
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions