Skip to content

YonLJ/async-js-loader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Load a JavaScript library asynchronously

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors