Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
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.
$ npm i asynz
$ bower i asynz
### Example: loading a dynamic library
import asynz from "asynz";
const src = 'https://code.jquery.com/jquery-2.2.1.js';
loadLibrary(src);
async loadLibrary() {
console.log(typeof window.jQuery === "undefined");
let script = await asynz(src);
$('body').html('jQuery is now available');
}
### 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 asynz(src, attrs);
console.log($('#jquery').attr('data-api-key') === 123);
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
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 asynz(erroredSrc);
// Do stuff
} catch(e) {
console.log('Error :(', e);
}
### Promise style You can also use Promise style if you prefer
import asynz from "asynz"
const src = 'https://code.jquery.com/jquery-2.2.1.js';
asynz(src).then(script => {
console.log(jQuery);
}).catch(console.log.bind(console));
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
{
"presets": ["es2015", "stage-0"],
"plugins": ["transform-async-to-generator"]
}
Include in your source
require("babel-core/register");
require("babel-polyfill");
FAQs
Hipster way of load async scripts in the browser
The npm package asynz receives a total of 2 weekly downloads. As such, asynz popularity was classified as not popular.
We found that asynz demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.