Programmatically Update the Status of a Post Using Gutenberg JS

In a WordPress project I am building, I needed a way to programmatically update the post status of a post on the edit screen, specifically in the Gutenberg editor.

Before arriving at the solution below, I first struggled to figure this out. I was trying to call the updatePost method with a post object and modified status, then calling savePost but what kept happening was the post would revert to draft status.

It turns out that to update a post status (and other post-specific values), you need to call editPost first followed by savePost right after saving the changes.

wp.data.dispatch('core/editor').editPost({
    status: 'publish'
});

wp.data.dispatch('core/editor').savePost();

It seems so simple in hindsight, but it took me a while to work this out. If you are reading this, chances are you got stuck on this simple task too.