question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MapView doesnt work with async/await

See original GitHub issue

This probably applies to all interfaces that inherits from corePromise If you try to return MapMiew instance in a promise using async& await you get compilation error: Return expression in async function does not have a valid callable 'then' member.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
eric-parsonscommented, Jun 27, 2017

You can convert any promise-like object such as IPromise into an ES6 promise by using Promise.resolve:

await Promise.resolve(mapView);

However, there is another issue that you will immediately run into. Several objects in the JavaScript API are promises that fullfill with themselves when resolved. When attempting to resolve such promises, promise implementations that follow the A+ promise specification must recurse forever (or optionally detect the cycle and reject with a TypeError). This applies to most browser’s native implementations and several popular polyfills such as Bluebird. If you run the above code in e.g. Google Chrome, it will simply hang forever. To get around this, I created the following helper function (written in TypeScript, which I infer is what you’re using):

/**
 * Returns a promise that is fulfilled when the given Esri API object is ready.
 * @param object The object to wait for.
 */
export function ready(object: any): Promise<void> {
    return new Promise<void>((resolve, reject) => {
        if (object && typeof object.then === "function") {
            object.then(() => resolve(), reject);
        } else {
            resolve();
        }
    });
}

The trick is that it returns a Promise<void> instead of e.g. Promise<MapView>. The usage would then be:

await ready(mapView);

You might also find this helper useful as well:

/**
 * Loads the given Esri API object and returns a promise that is fulfilled when
 * the object is loaded.
 * @param object The object to load.
 */
export async function load(object: any): Promise<void> {
    if (object && typeof object.load === "function") {
        object = object.load();
    }

    await ready(object);
}

Then you can do this if you need to load e.g. a layer:

await load(layer);
1reaction
dasacommented, Jan 2, 2018
Read more comments on GitHub >

github_iconTop Results From Across the Web

Returning MapView instance from async function (or passing it ...
Somes ArcGIS JS API classes looks like promises as their instance present a then() method.
Read more >
Swift Concurrency and Runtime - Esri Community
Introducing the ArcGIS Runtime async/await package. We're pleased to present a handy set of open source Swift files that fill the gap, ...
Read more >
Programming patterns | ArcGIS Maps SDK for JavaScript 4.25
After getting the ArcGIS Maps SDK for JavaScript, use require() to asynchronously load ArcGIS Maps SDK for JavaScript classes into an application.
Read more >
Implementing address autocomplete using SwiftUI and MapKit
I found it curious — to know how we can limit ourselves to using only SwiftUI and MapKit to solve a similar problem....
Read more >
esri-loader - npm
A tiny library to help load ArcGIS API for JavaScript modules in ... Dojo's loader to require the classes loadModules(['esri/views/MapView', ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found