You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
WP 6.7RC-1: wp.mediaUtils.uploadMedia no longer returns Promise #66551
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 ):
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 uploadMediaasync 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.
Activity
Mamaduka commentedon Nov 5, 2024
@swissspidy, could this be related to the recent changes made to this utility method?
swissspidy commentedon Nov 5, 2024
Yes. This was introduced in #64784.
Previously, it was an async function (thus returning a Promise with a
voidreturn value) because it uploaded files in aforloop with anawaitin it. That means all uploads were done in sequence, which is inefficient.As part of the conversion, this was changed to use
Array.prototype.mapso the uploads are queued in parallel. That also meant there was no longer a need for the function to beasync.Note: awaiting
uploadMedia()was never useful anyway, because it never returned anything. It solely relies on theonFileChangeandonErrorcallbacks. The readme also doesn't suggest usingawait.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
onFileChangeoronErroris 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
uploadMediaasyncagain, 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.