Simple promise-based script loader for modern browsers
npm install --save promisescript
var promisescript = require'promisescript';return promisescripturl: '//www.google.com/recaptcha/api/js/recaptcha_ajax.js'type: 'script'exposed: 'Recaptcha'thenreturnRecaptchacreate'0123456789abcdef' 'recaptcha_widget'theme: 'clean'callback: resolve;;;loadRecaptchathenconsole.logRecaptchaget_challenge;catchconsole.error'An error loading or executing Recaptcha has occured: ' emessage;;
A source object is fully specified when it contains three properties:
url, the URL that should be fetched
type, either "script" or "style", which defines the type of the URL being fetched.
exposed, a string that is checked against the global object to determine if the script successfully loaded. A deeply nested property can be checked by providing a dot-separated string. This property is not required, but required to check if a script loaded successfully in Internet Explorer, as this browser does not have an error event.
This module accepts a single source object, and returns a single Promise that resolve if successfully loaded, or rejected if it fails.
Additionally this module can accept multiple source objects as an array. An array of Promises is returned, in the same order as provided.
If you’re feeling lazy, and don’t care about Internet Explorer support, you can provide a string or an array of strings that represents script or style URLs. When utilizing this functionality, this module assumes URLs representing styles end in “.css”, and all others are assumed to be scripts.
To avoid fetching the same URL multiple times, the Promise is cached, which is fine behavior most use cases.
However, this cache can be cleared by calling
- If you make multiple requests to the same URL, it will only be fetched once and the same Promise will be returned.
- The Promises returned by this module meet the ES6 standard, and are thus barebones; you may wish to coerce it into a Promise of your favorite library.