While you can use classic event callbacks (.on('load', ...)), the SDK provides modern async methods that return Promises. This allows for a linear, non-nested execution flow in async functions. These methods are available for events that occur only once in the lifecycle:
onLoadAsync(): Resolves when the load event fires.onReadyAsync(): Resolves when the ready event fires.onLoadWithTerrainAsync(): Resolves when the loadWithTerrain event fires.
async function init() {
const map = new Map({
container,
center: [2.34804, 48.85439],
zoom: 14,
terrain: true,
});
// Wait for the promise to resolve instead of using a callback
await map.onLoadWithTerrainAsync();
// Now safe to add sources, layers, or run animations
map.addSource("my-source", { type: "geojson", data: ... });
map.flyTo({ center: [-0.09956, 51.50509], zoom: 12.5 });
}