Skip to content

Technical review: Document scroll promise return values#44515

Open
chrisdavidmills wants to merge 8 commits into
mdn:mainfrom
chrisdavidmills:scroll-promises
Open

Technical review: Document scroll promise return values#44515
chrisdavidmills wants to merge 8 commits into
mdn:mainfrom
chrisdavidmills:scroll-promises

Conversation

@chrisdavidmills

Copy link
Copy Markdown
Contributor

Description

In Chrome 150 onwards, the standard Element and Window scroll methods now return promises. See https://chromestatus.com/feature/5082138340491264. This is particularly useful when a scroll operation is not instantaneous (e.g. you've got scroll-behavior: smooth; set on the scrolling container), and you want to programmatically react to the scroll operation finishing, for example, by updating the UI.

Affected methods are:

  • Element.scroll()
  • Element.scrollBy()
  • Element.scrollIntoView()
  • Element.scrollTo()
  • Window.scroll()
  • Window.scrollBy()
  • Window.scrollTo()

My tests revealed that only the standard methods return a promise. Non-standard methods (Element.scrollIntoViewIfNeeded(), Window.scrollByLines(), Window.scrollByPages()) do not, at least not yet.

This PR adds content about the promise return values to each relevant method page, and makes some other improvements.

Motivation

Additional details

Related issues and pull requests

@chrisdavidmills chrisdavidmills requested a review from a team as a code owner June 19, 2026 13:07
@chrisdavidmills chrisdavidmills requested review from hamishwillee and removed request for a team June 19, 2026 13:07
@github-actions github-actions Bot added the Content:WebAPI Web API docs label Jun 19, 2026
@chrisdavidmills chrisdavidmills changed the title Document scroll promise return values Technical review: Document scroll promise return values Jun 19, 2026
@github-actions github-actions Bot added the size/l [PR only] 501-1000 LoC changed label Jun 19, 2026
@github-actions

github-actions Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor
Preview URLs (7 pages)
External URLs (7)

URL: /en-US/docs/Web/API/Element/scroll
Title: Element: scroll() method


URL: /en-US/docs/Web/API/Element/scrollBy
Title: Element: scrollBy() method


URL: /en-US/docs/Web/API/Element/scrollIntoView
Title: Element: scrollIntoView() method


URL: /en-US/docs/Web/API/Element/scrollTo
Title: Element: scrollTo() method


URL: /en-US/docs/Web/API/Window/scroll
Title: Window: scroll() method


URL: /en-US/docs/Web/API/Window/scrollBy
Title: Window: scrollBy() method


URL: /en-US/docs/Web/API/Window/scrollTo
Title: Window: scrollTo() method

(comment last updated: 2026-06-23 01:30:32)

Comment thread files/en-us/web/api/element/scroll/index.md Outdated
Comment thread files/en-us/web/api/element/scrollby/index.md Outdated
Comment thread files/en-us/web/api/element/scrollintoview/index.md Outdated
Comment thread files/en-us/web/api/element/scrollintoview/index.md Outdated
Comment thread files/en-us/web/api/element/scrollto/index.md Outdated
Comment thread files/en-us/web/api/window/scroll/index.md Outdated
Comment thread files/en-us/web/api/window/scrollto/index.md Outdated
Comment thread files/en-us/web/api/window/scrollby/index.md Outdated
Comment thread files/en-us/web/api/window/scroll/index.md Outdated
Comment thread files/en-us/web/api/window/scroll/index.md Outdated
Co-authored-by: Hamish Willee <hamishwillee@gmail.com>
Comment thread files/en-us/web/api/element/scrollintoview/index.md Outdated
Comment thread files/en-us/web/api/element/scrollintoview/index.md Outdated
Comment thread files/en-us/web/api/element/scrollintoview/index.md Outdated
Comment thread files/en-us/web/api/element/scroll/index.md
Comment on lines +149 to +155
```js
scrollBtn.addEventListener("click", async () => {
toolbar.className = "fade-out";
await section.scroll(0, 1000);
console.log("Scroll finished");
toolbar.className = "fade-in";
});

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. The examples don't work yet. IMO would prefer in-page examples to these kinds of link elsewhere examples.
  2. IFF the return type is a promise that resolves with undefined (as per spec) the example is good. If the promise actually resolves with interrupted as the docs say then this should output the value instead.

Ditto everywhere.,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. See Add scroll-promises examples dom-examples#386 for the examples. I've just updated the PR so that the example event handlers output an indication of whether the scroll was interrupted or not, rather than just a static log message.
  2. I normally agree that in-page live examples are better. However, in this case, you've got seven pages to document this behavior on, and the promise return value, while significant, is not the be-all and end-all of the methods; it is more of a useful addition. I don't really want to repeat the same nearly identical code on seven pages; instead, I've created two examples — one to cover Element.x and one to cover Window.x, and just shown the pertinent bits on each page.

I will update the example description on each page to match the changes I've made to the example repo before passing this back to you again.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK; descriptions and code snippets updated as per the dom-examples changes.

hamishwillee and others added 3 commits June 22, 2026 09:55
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

@hamishwillee hamishwillee left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good @chrisdavidmills but the example have a flaw mdn/dom-examples#386 (review)

Appreciate you don't want to do in-page examples, but you might still consider EmbedGHLiveSample so that users don't have to leave the page (just a thought, haven't tried it).

Approving so you can merge this and your examples together when the examples are ready.

@chrisdavidmills

Copy link
Copy Markdown
Contributor Author

This looks good @chrisdavidmills but the example have a flaw mdn/dom-examples#386 (review)

Appreciate you don't want to do in-page examples, but you might still consider EmbedGHLiveSample so that users don't have to leave the page (just a thought, haven't tried it).

Approving so you can merge this and your examples together when the examples are ready.

Thanks, @hamishwillee. I'll wait for your comment on the dom-examples repo fix before I go any further on this.


Load our [element methods demo](https://mdn.github.io/dom-examples/scroll-promises/element-methods/) ([see source code](https://github.com/mdn/dom-examples/tree/main/scroll-promises/element-methods)) in a new tab and click the buttons to see the scrolling behavior. Note how the toolbar fades out when a button is pressed, and fades in again once the smooth scrolling is finished.

Try pressing one button and then quickly pressing another button before the first scrolling operation has finished. Open your browser's JavaScript console and note how, in these cases, the scrolling is reported as interrupted.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May we perhaps use an animation to show the interruption? What if the scroller and/or the button flashes briefly?

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

Labels

Content:WebAPI Web API docs size/l [PR only] 501-1000 LoC changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants