Skip to content

WP 6.7RC-1: wp.mediaUtils.uploadMedia no longer returns Promise #66551

Closed

Description

@elzadj

Description

Hi all!

We were testing our plugin on WordPress 6.7RC-1, and noticed that our media uploader no longer works. Managed to track it to the differences in wp.mediaUtils.uploadMedia between 6.6.2 and 6.7RC-1.

We use this simple code for tests (based on the readme ):

wp.mediaUtils.uploadMedia( {
	filesList: [ new File( [ 'foo' ], 'foo.txt', { type: 'text/plain' } ) ],
	onFileChange: ( [ fileObj ] ) => console.log( fileObj ),
	onError: console.error,
} );

Compare the results between versions:

6.7RC-1:
Image

6.6.2:
Image

We can adjust code in our plugin to make it work on both versions, but would be nice to know if this change was intentional.
Thanks.

Step-by-step reproduction instructions

  1. Update to 6.7RC-1
  2. Open Gutenberg Editor.
  3. Run
wp.mediaUtils.uploadMedia( {
	filesList: [ new File( [ 'foo' ], 'foo.txt', { type: 'text/plain' } ) ],
	onFileChange: ( [ fileObj ] ) => console.log( fileObj ),
	onError: console.error,
} ); 

in your console.
4. Check the result.

Screenshots, screen recording, code snippet

No response

Environment info

  • WordPress 6.7RC-1
  • Chrome Version 130.0.6723.58 (Official Build) (64-bit)
  • Ubuntu 24.04.02

Please confirm that you have searched existing issues in the repo.

  • Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

  • Yes

Please confirm which theme type you used for testing.

  • Block
  • Classic
  • Hybrid (e.g. classic with theme.json)
  • Not sure

Activity

  1. added
    [Type] BugAn existing feature does not function as intended
    on Oct 29, 2024
  2. Mamaduka commented on Nov 5, 2024

    @Mamaduka
    Member

    @swissspidy, could this be related to the recent changes made to this utility method?

  3. swissspidy commented on Nov 5, 2024

    @swissspidy
    Member

    Yes. This was introduced in #64784.

    Previously, it was an async function (thus returning a Promise with a void return value) because it uploaded files in a for loop with an await in it. That means all uploads were done in sequence, which is inefficient.

    As part of the conversion, this was changed to use Array.prototype.map so the uploads are queued in parallel. That also meant there was no longer a need for the function to be async.

    Note: awaiting uploadMedia() was never useful anyway, because it never returned anything. It solely relies on the onFileChange and onError callbacks. The readme also doesn't suggest using await.

    I'm curious to learn why your code doesn't work anymore, since there is no return value. It sounds like you might have just relied on the coincidence that onFileChange or onError is called on the next tick in the event loop as soon as the Promise settled, instead of relying on the callback functions.

    A short term fix would be to make uploadMedia async again, but that won't help you in the future, where files will first be processed in the browser in a queue-like system before sending them to the server (see #61447). So I'd advise against that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs Technical FeedbackNeeds testing from a developer perspective.[Feature] MediaAnything that impacts the experience of managing media[Type] BugAn existing feature does not function as intended

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions