What is yarn?
Yarn is a fast, reliable, and secure dependency management tool that is used to manage project dependencies in JavaScript and Node.js environments. It caches every package it downloads, so it never needs to download the same package again. It also parallelizes operations to maximize resource utilization, and it uses checksums to verify the integrity of every installed package before its code is executed.
What are yarn's main functionalities?
Adding a dependency
This command adds the lodash package to the project's dependencies.
yarn add lodash
Upgrading a dependency
This command upgrades the react package to its latest version according to the version range specified in the package.json file.
yarn upgrade react
Removing a dependency
This command removes the moment package from the project's dependencies.
yarn remove moment
Installing all dependencies
This command installs all the dependencies listed in the project's package.json file.
yarn install
Locking down dependency versions
This command installs dependencies based on the yarn.lock file without generating a new lock file, ensuring reproducibility across installations.
yarn install --frozen-lockfile
Other packages similar to yarn
npm
npm is the default package manager for Node.js and is bundled with it. It serves a similar purpose to Yarn, allowing users to install, share, and manage dependencies in their projects. Compared to Yarn, npm has a different CLI syntax and handles package installations and lock files differently. npm has improved its performance in recent versions, but Yarn is often cited for its faster and more reliable installations due to its caching and parallelization features.
pnpm
pnpm is another alternative to Yarn that focuses on performance and disk space efficiency. It uses a content-addressable filesystem to store packages, which means that a package version is saved only once on a disk and then hard-linked into the node_modules directories of projects that use it. This results in faster installations and less disk space usage. pnpm also has a strictness that prevents implicit dependencies, which can lead to more predictable and secure projects.
yarn
Yarn is a simple asynchronous routing pattern that makes it easy to write asynchrounous code in linear fashion.
If you like my work, buy me a Red Bull or Macbook Air or something. xP
Bitcoin Address: 1ADbZY8XBJsqjPMGrEHf9C9QeNvrBmhL3K
Setup
To set up yarn on your Node.js server use npm.
npm install yarn
If you want to use yarn on the client-side (browser), just include yarn.js into your page.
<script type='text/javascript' src='yarn.js'></script>
Using yarn
Using yarn is simple. First, you have to do is require the yarn function (if you're using it client-side, this isn't necessary).
var yarn = require('yarn');
Then, you can call yarn and pass it an initial function, and then chain sets of functions after.
yarn
(function(){
this(0)();
})
(function(){
},
function(){
});
In the initial function (the first one), this
is a function which returns one of next functions in the next set of functions. For example, this(0)
would return the first function, and this(1)
would return the second function. Every function in the series has a this
function which returns one of the functions in the next set.
yarn
(function(){
this(Math.round(Math.random()))();
})
(function(){
this(0)();
},
function(){
this(1)();
})
(function(){
console.log('made it to the first of final set');
},
function(){
console.log('made it to the second of final set');
});
Here is an example of how you could use yarn with asynchronous callbacks.
yarn
(function(){
doSomethingAsynchronous(this());
})
(function(err){
// this(0) returns error function, and this(1) returns success function
if (err)
this(0)();
else
this(1)();
})
(function error(){
alert('Aww...');
},
function success()
alert('Yay!!!');
});
function doSomethingAsynchronous(callback) {
setTimeout(function(){
if (Math.round(Math.random()) == 0)
callback(true);
else
callback(false);
}, 1000);
}
Another thing to note is that this()
(called with no index passed) is synonymous to this(0)
.
to be conitnued...possibly