Skip to content
This repository has been archived by the owner on Nov 6, 2024. It is now read-only.

Commit

Permalink
allow consumers to override the Promise implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwayson committed Nov 10, 2017
1 parent dc88d9a commit a62b40b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,21 @@ Here are some applications that use this library (presented by framework in alph

## Dependencies

This library doesn't have any external dependencies, but it expects to be run in a browser (i.e. not Node.js). Since v1.5 asynchronous functions like `loadScript()` and `loadModules()` return [`Promise`s](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise), so if your application has to support [browers that don't support Promise (i.e. IE)](https://caniuse.com/#search=promise), then you should consider using a [Promise polyfill](https://www.google.com/search?q=promise+polyfill), ideally [only when needed](https://philipwalton.com/articles/loading-polyfills-only-when-needed/).
This library doesn't have any external dependencies, but it expects to be run in a browser (i.e. not Node.js). Since v1.5 asynchronous functions like `loadScript()` and `loadModules()` return [`Promise`s](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise), so if your application has to support [browers that don't support Promise (i.e. IE)](https://caniuse.com/#search=promise) you have a few options.

Alternatively, you can still use `bootstrap()` and `dojoRequire()` which are the callback-based equivalents of the above functions. See the [v1.4.0 documentation](https://github.com/Esri/esri-loader/blob/v1.4.0/README.md#usage) for how to use the callback-based API, but _keep in mind that these functions have been deprecated and will be removed at the next major release_.
If there's already a Promise implementation loaded on the page you can configure esri-loader to use that implementation. For example, in [ember-esri-loader](https://github.com/Esri/ember-esri-loader), we configure esri-loader to use the RSVP Promise implementation included with Ember.js.

```js
init () {
this._super(...arguments);
// have esriLoader use Ember's RSVP promise
esriLoader.utils.Promise = Ember.RSVP.Promise;
},
```

Otherwise, you should consider using a [Promise polyfill](https://www.google.com/search?q=promise+polyfill), ideally [only when needed](https://philipwalton.com/articles/loading-polyfills-only-when-needed/).

Finally, for now you can still use `bootstrap()` and `dojoRequire()` which are the callback-based equivalents of the above functions. See the [v1.4.0 documentation](https://github.com/Esri/esri-loader/blob/v1.4.0/README.md#usage) for how to use the callback-based API, but _keep in mind that these functions have been deprecated and will be removed at the next major release_.

## Issues

Expand Down
10 changes: 8 additions & 2 deletions src/esri-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ export interface IBootstrapOptions {
dojoConfig?: { [propName: string]: any };
}

// allow consuming libraries to provide their own Promise implementations
export const utils = {
Promise: window['Promise']
};

export interface ILoadScriptOptions {
url?: string;
// NOTE: stole the type definition for dojoConfig from:
Expand All @@ -96,7 +101,7 @@ export function loadScript(options: ILoadScriptOptions = {}): Promise<HTMLScript
options.url = DEFAULT_URL;
}

return new Promise((resolve, reject) => {
return new utils.Promise((resolve, reject) => {
let script = getScript();
if (script) {
// the API is already loaded or in the process of loading...
Expand Down Expand Up @@ -146,7 +151,7 @@ export function loadScript(options: ILoadScriptOptions = {}): Promise<HTMLScript

// wrap dojo's require() in a promise
function requireModules(modules: string[]): Promise<any[]> {
return new Promise((resolve, reject) => {
return new utils.Promise((resolve, reject) => {
// If something goes wrong loading the esri/dojo scripts, reject with the error.
const errorHandler = window['require'].on('error', reject);
window['require'](modules, (...args) => {
Expand Down Expand Up @@ -247,6 +252,7 @@ export default {
isLoaded,
loadScript,
loadModules,
utils,
// TODO: remove these the next major release
bootstrap,
dojoRequire
Expand Down

0 comments on commit a62b40b

Please sign in to comment.