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.

loadModules examples in the README - async/await?

See original GitHub issue

To me, the way @jwasilgeo is using loadModules with async/await here is much more readable. Do others agree? Now that async await is pretty mainstream (sorry IE11), I’m wondering if we should switch all our examples in the README to use this style (we could still include one Promises style example just to cover all bases)

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

4reactions
tomwaysoncommented, Dec 31, 2020

I think we should do this. It’s time.

4reactions
gavinrcommented, Jan 17, 2020

One note that I’ve realized since I posted the original issue:

sometimes part of the “simplicity” of async/await examples is actually just because the promise-based includes error handling where the async/await example [incorrectly] leaves it out.

If we take an example from our actual README:

// component.js
import { loadModules } from 'esri-loader';

// this will lazy load the ArcGIS API
// and then use Dojo's loader to require the map class
loadModules(['esri/map'])
.then(([Map]) => {
  // create map with the given options at a DOM node w/ id 'mapNode'
  let map = new Map('mapNode', {
    center: [-118, 34.5],
    zoom: 8,
    basemap: 'dark-gray'
  });
})
.catch(err => {
  // handle any script or module loading errors
  console.error(err);
});

It would be tempting and satisfying to change it to async/await without error handling:

// component.js
import { loadModules } from 'esri-loader';

// this will lazy load the ArcGIS API
// and then use Dojo's loader to require the map class
const [Map] = await loadModules(['esri/Map']);

// create map with the given options at a DOM node w/ id 'mapNode'
let map = new Map('mapNode', {
  center: [-118, 34.5],
  zoom: 8,
  basemap: 'dark-gray'
});

… but that’s not equivalent to the original promise-based example! Making it equivalent (including error catching):

// component.js
import { loadModules } from 'esri-loader';

// this will lazy load the ArcGIS API
// and then use Dojo's loader to require the map class
try {
  const [Map] = await loadModules(['esri/Map']);

  // create map with the given options at a DOM node w/ id 'mapNode'
  let map = new Map('mapNode', {
    center: [-118, 34.5],
    zoom: 8,
    basemap: 'dark-gray'
  });

} catch (err) {
  // handle any script or module loading errors
  console.error(err);
}

… which makes it closer in complexity to the promise-based example. I would argue the async/await-example-with-try-catch example is still slightly better readability, but only marginally.

Read more comments on GitHub >

github_iconTop Results From Across the Web

getting fs.readme to work with async await and promisify
1 Answer 1 · You are creating a promisified function but you are using it with a callback. · I can that the...
Read more >
esri-loader | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >
Async/await - The Modern JavaScript Tutorial
The keyword await makes JavaScript wait until that promise settles and returns its result. Here's an example with a promise that resolves in...
Read more >
async-await-utils - npm
async-await -utils. 4.0.0 • Public • Published 5 months ago. Readme · Code Beta · 21 Dependencies · 20 Dependents · 16 Versions ......
Read more >
Lazy-Loading Angular Modules (Ivy and Async Await)
In this post you'll learn how to Lazy Load an Angular module. Lazy loading means that our code isn't downloaded by the browser...
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