asynz
Hipster way of load async script in the browser
Just a tiny module to load scripts in the browser. It also has support for script attributes and cache.
Install
$ npm i asynz
$ bower i asynz
Example: loading a dynamic library
;const src = 'https://code.jquery.com/jquery-2.2.1.js'; ; async { console; let script = await ; ;}
Passing attributes
const src = 'https://code.jquery.com/jquery-2.2.1.js';const attrs = id: 'jquery' async: true defer: true foo: 'bar' 'data-api-key': 123; let script = await ; console;
Notice that when you pass script
properties like async or defer, azync won't add them as an attribute, instead will set the value directly to the script, e.g. script.async === true
Error handling
One of the cool things of the async functions, is that you can handle errors like it was synchronous code.
const erroredSrc = 'http://foourl'; try await ; // Do stuff catche console;
Promise style
You can also use Promise style if you prefer
const src = 'https://code.jquery.com/jquery-2.2.1.js'; ;
Miscellaneous
In order to use async functions today you will need to install/include some stuff in your build process
Needed packages
$ npm i babel-cli babel-polyfill babel-plugin-transform-async-to-generator babel-preset-es2015 babel-preset-stage-0 --save-dev
.babelrc
Include in your source
;;