Coder Social home page Coder Social logo

jsutils's Introduction

Array extensions

firstOrNull

This method is added to the Array prototype. This returns the first element of an array or null if the array is empty.

Examples:

const myArray = [1, 2, 3];
console.log(myArray.firstOrNull()); // returns 1
const myArray = [];
console.log(myArray.firstOrNull()); // returns null;

lastOrNull

This method is added to the Array prototype. This returns the last element of an array or null if the array is empty.

Examples:

const myArray = [1, 2, 3];
console.log(myArray.lastOrNull()); // returns 3
const myArray = [];
console.log(myArray.lastOrNull()); // returns null;

Future

This is a simple Future implementation based on deasync. This is an easy way to turn an asynchronous process in to a synchronous process.

Example:

const f = new Future();

setTimeout(() => {
  if (somethingBadHappened) {
    f.throw(new Error('Bad'));
  }
  f.return(5);
}, 2000);

// Here we wait for the result (5), which will be placed in the output or an exception is thown.
const output = f.wait;

parallel

Processes a list of functions in parallel and calls the callback when all functions have finished or when an error occures.

Syntax:

parallel(tasks, max = -1, callback);

Example:

const tasks = [];
tasks.push((cb) => {
  // do something
  cb(null, 5);
});

tasks.push((cb) => {
  // do something
  cb(null, 3);
});

parallel(tasks, (err, 5, results) => {
  // contains is now [5, 3] unless an error occured.
});

series

Processes a list of functions serial and calls the callback when all functions have finished or when an error occures.

Syntax:

series(tasks, callback);

Example:

const tasks = [];
tasks.push((cb) => {
  // do something
  cb(null, 5);
});

tasks.push((cb) => {
  // do something
  cb(null, 3);
});

series(tasks, (err, results) => {
  // contains is now [5, 3] unless an error occured.
});

jsutils's People

Contributors

sixpaq avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.