-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce "await map.until(event)" syntax for easier async #10192
Comments
First, yep, a promise interface would be extremely welcome. I'm a little bit surprised that this would be a "breaking" change: It also isn't obvious to me that making However, if that's the case, fine. I have one more name suggestion
I don't feel very strongly about the name choices. I maybe marginally prefer either |
Slight further comment on naming: If the long term plan is for If that isn't going to happen, maybe |
This is actually a very interesting suggestion — somehow I haven't considered conditionally returning a promise if the callback isn't provided. This would definitely not be a breaking change, and ergonomically makes a lot of sense. The only concern is that we can't do that for similar methods like |
How about: await new mapboxgl.Map(...);
await map.addLayer(...);
await map.flyTo(...); and yes, as @stevage mentioned above, these methods can return a promise if there is no callback passed (e.g. |
@Bravecow the ones you listed would be breaking changes, which we're considering but for some later time, together with a bigger overhaul of the API. For now, I'd like to find a non-breaking middle ground. |
Also, I'm not sure that And it's not clear what the semantics would be in any case - wait until the map is completely loaded, or just until it's safe to add layers (which is what users normally want). Similarly with |
|
Maybe this would be a nice way to sidestep the giant mess around events and knowing when it is safe to add a layer, by introducing:
which returns a promise which returns when you can add a layer. (This is a bit of a tangent from the actual issue here though.) |
I like the idea of allowing In conjunction with #10191, developers should have a much simpler and more modern code style for implementing maps. #10191 could even obviate much of the need for listening to |
Now that we no longer support IE11, we can take advantage of newer browser capabilities such as native promises and
async
/await
to make programming GL JS easier. To start doing so in a backwards-compatible way, I propose adding a method toEvented
-inherited classes such asMap
that's just a shortcut tonew Promise(resolve => this.once(event, resolve))
.This is a very tiny change, but can make the ergonomics of setting up multi-step animations and similar things much easier — instead of a bunch of nested callbacks, you would be able to write code linearly like this:
I explored a similar style of programming in my video export example #10172, and it feels awesome, so how does everyone feel about adding it? In the future, we might consider a full overhaul of the API (discussed in #3964) to make all async methods return a promise, but that's a substantial effort and also significantly breaking. But we could get halfway there in terms of ergonomics with a one-line change.
Alternative names:
cc @stevage @andrewharvey
The text was updated successfully, but these errors were encountered: