Get the ID of a page in Parent combobox in editor

After some time reading the Editor Documentation I found the proper solution. This is how you can listen to changes and get the id of the Parent Combobox select in the WP Editor:

wp.data.subscribe( function () {
  var newParent = wp.data.select('core/editor').getEditedPostAttribute('parent');
  console.log(newParent);
} );

wp.data.subscribe is called every single time a change occurs in the current state while editing posts in the editor, so the listener function is called every single time. We can avoid that with a simple variable check when there is an actual change to the field we want. It behaves as Redux subscribe without unsubscribe and only one listener.

To also check for the current post type we are editing we can use getCurrentPostType:

wp.data.select('core/editor').getCurrentPostType();

This is the full code for this problem for future reference:

if (wp.data.select('core/editor').getCurrentPostType() == 'cpt_name') {
 const getPostParent = () => wp.data.select('core/editor').getEditedPostAttribute('parent');

 // set initial parent
 let postParent = getPostParent();

 wp.data.subscribe(() => {

    // get current parent
    const newPostParent = getPostParent();    

    // only if parent changes
    if( postParent !== newPostParent ) {
      // Do what we want after parent changed
         toggleFields();
    }

    // update the variable
    postParent = newPostParent;

 });

}