Trycatch

intermediate4 of 7

Learn about trycatch in fp-ts io

Code Editor

04-trycatch.exercise.ts

💻
Loading editor...
Preparing Monaco Editor with TypeScript support

Test Results

Requirements

describe('IOEither tryCatch', () => {
  it('parses valid JSON', () => {
    const result = parseJSON('{"name":"Alice"}')()
    expect(E.isRight(result)).toBe(true)
    if (E.isRight(result)) {
      expect(result.right).toEqual({ name: 'Alice' })
    }
  })

  it('fails on invalid JSON', () => {
    const result = parseJSON('{invalid}')()
    expect(E.isLeft(result)).toBe(true)
  })

  it('parses valid number', () => {
    const result = parseNumber('42')()
    expect(E.isRight(result)).toBe(true)
    if (E.isRight(result)) {
      expect(result.right).toBe(42)
    }
  })

  it('fails on invalid number', () => {
    const result = parseNumber('not-a-number')()
    expect(E.isLeft(result)).toBe(true)
  })
})
🧪

Ready to Test?

Click "Run Tests" to see how your code performs

Pro Tips

💡 Stuck? Here's what to try:

  • • Read the comments in the code carefully
  • • Run tests frequently to get feedback
  • • Check the fp-ts documentation
  • • Use the solution if you need help

🚀 Learning Approach:

  • • Focus on understanding, not just solving
  • • Experiment with different approaches
  • • Think about real-world applications
  • • Build on previous exercises