# Folding functions

**URL:** <https://discuss.codemirror.net/t/folding-functions/4189>\
**Category:** v6\
**Created:** [March 28, 2022, 4:21pm UTC](https://discuss.codemirror.net/t/folding-functions/4189 "2022-03-28T16:21:36Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![mercmobily](https://discuss.codemirror.net/user_avatar/discuss.codemirror.net/mercmobily/32/1038_2.png) [@mercmobily](https://discuss.codemirror.net/u/mercmobily)\
**Post date:** [March 29, 2022, 9:51am UTC](https://discuss.codemirror.net/t/folding-functions/4189/9 "2022-03-29T09:51:00Z")

</div>

Wait you wrote _synchronously_! Sorry again.  
So I tried this:

```auto
import { ensureSyntaxTree } from '@codemirror/language'

  set value (v) {
    const state = this.view.state

    this.view.dispatch({
      changes: { from: 0, to: state.doc.length, insert: v || '' }
    })

    ensureSyntaxTree(this.view.state, state.doc.length, 5000)
    foldAll(this.view)
  }

```

but no luck.

I am not sure listening to updates will work for me, since I need to trigger the folding only when I set the editor to a new value (for example when you navigate onto that particular page in an SPA).

(I tried to figure out how to go this route, and I realise that [UpdateListener](https://codemirror.net/6/docs/ref/#view.EditorView%5EupdateListener) is the key, but I haven’t managed to figure out how to actually use it).

---

_[View the full topic](https://discuss.codemirror.net/t/folding-functions/4189)._
