async-js-loader
TypeScript icon, indicating that this package has built-in type declarations

0.1.3 • Public • Published

async-js-loader

NPM Version License: MIT NPM Downloads

Load a JavaScript library asynchronously and the same library is loaded only once.

Pre-requisites

The loaded JavaScript library uses var to declare variables. In other words, a variable in the JavaScript library can be referenced using a property of the window. It can only be used in Browser.

Install

npm i async-js-loader --save

Usage

You can use async-js-loader in JavaScript or TypeScript.

async/await + await-to-js

import to from 'await-to-js';
import loader from 'async-js-loader';
// If you use CommonJS (i.e NodeJS environment), it should be:
// const loader = require('async-js-loader').default;
 
async loadAmapLibrary() {
  const [err, AMap] = await to(loader('AMap', 'https://webapi.amap.com/maps?v=1.4.15&key=license'));
  if(err) {
    console.log(err);
    return;
  }
  console.log(AMap);
  // If there are other variables or functions in this JavaScript library, you can get them from "window".
  // console.log(window[variableName])
}

Promise

import loader from 'async-js-loader';
 
loadAmapLibrary() {
  loader('AMap', 'https://webapi.amap.com/maps?v=1.4.15&key=license')
    .then(AMap => console.log(AMap))
    .catch(e => console.log(e));
}

License

MIT © Tintin

keywords

async script load promise

Package Sidebar

Install

npm i async-js-loader

Weekly Downloads

1

Version

0.1.3

License

MIT

Unpacked Size

5.48 kB

Total Files

5

Last publish

Collaborators

  • yonlj