E2020 dynamic imports in JavaScript

Escmascript Is a standard from which JavaScript and typescript are both derived from.

ES2020 is out it comes with it new ways of writing JavaScript code.

Dynamic imports enable importing dependencies in a rather dynamic way.

Initially JavaScript developers were restricted to statically load their dependencies for instance;

import { property } from 'dependency'

On runtime the JavaScript engine loads the required dependencies and makes them accessible globally.

However that is a rigid way of writing code.

Dynamic imports are  relatively more flexible therefore you can included them at any segment within your program.

For instance if you want to load file system module only if a certain criteria is met you would do as follows;

If(process.argv[2] == 'start')
Import fs from 'fs'

Dynamic imports return a promise use the syntax below to make use of them

import('fs')
.then(fs => {
   // consume module fs
})

Comments

Popular posts from this blog

Open-graph meta data tags introdution

What is 'this.' keyword in JavaScript

How to create zip archives in nodejs

what is 'array.includes' function in JavaScript

How to build a twitter retweet bot using JavaScript