Search code examples
ckeditorckeditor5

Why does CKEditor5 use a promise to initialize it?


Example:

ClassicEditor
.create( document.querySelector( '#editor' ) )
.then( editor => {
    console.log( editor );
} )
.catch( error => {
    console.error( error );
} );

Why would editor creation need to be asynchronous?


Solution

  • The editor initialization can be asynchronous because some editor features or editor UI may require asynchronous initialisation.

    I'm inconcrete on purpose here. As a framework developer, I don't know what kind of features the users of the framework will want to implement. However, I know some examples from the past:

    • an <iframe>-based editor (iframes are initialized asynchronously), i.e. an editor in which the content is edited within an <iframe>,
    • real-time collaboration features which need to retrieve the content from the server.

    If you're implementing a plugin which need to defer initialisation, then you can simply return a promise from its init() or afterInit() methods:

    class MyPlugin extends Plugin {
        init() {
            return new Promise( resolve => {
                // Call resolve() once your plugin is ready:
                resolve();
            } );
        }
    }