GithubHelp home page GithubHelp logo

simonifergan / promises-everywhere Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 1.5 MB

A utility library that is all about JavaScript Promises!

License: MIT License

TypeScript 100.00%
promises async tsdx

promises-everywhere's Introduction

Promises, Promises Everywhere npm version

A utility library that is all about JavaScript Promises!

About

I am always fascinated with things you can manipulate by using Promises. However, I was repeating my Promise conventions in every project or even multiple modules within the same project. Therefore, I decided that I should just write it once and use it as a library across all my projects.

Utilities

fake

A method that mimics an async function behavior. It takes an options configuration object as parameter, and returns either a resolved, or reject, promise.

interface Config {
  reject?: boolean;
  timeout?: number;
  response?: any;
}

Usage:

fake({
  timeout: 1500, // in Milliseconds, default timeout is 200
  response: { a: 1, b: 2 }, // Can be whatever you want it to be.
  reject: false, // Default value will always be false.
});

// with async/await
async function doSomething() {
  const myPromiseResponse = await fake({
    response: 'Got it',
  });
}

aggregate

A method that accepts a keyByArguments map, and a callback function. It will return an object where every key will contain the resolved or rejected promise from the callback function.

Usage:

const callbackFunction = async (id, filter) => {
  const response = await fetch('/api/some-path', {
    query: {
      id,
      filter,
    },
  }).then(res => res.json());
  return response;
};

async function DoSomething() {
  const keyByArgs = {
    first: {
      id: '123',
      filter: false,
    },
    second: {
      id: '234',
      filter: true,
    },
  };

  const myPromiseResponse = await aggregate(keyByArgs, callbackFunction);
}

wrap

A method that accepts a callback function, and wraps it with a Promise so it can be used within any async/await function closure.

Usage:

const savePost = (method, body) => {
  return fetch('/api/some-path', {
    method,
    body,
  }).then(res => res.json());
};

async function DoSomething(blogPost) {
  const saveBlogPost = wrap(savePost);

  const savedBlogPost = await saveBlogPost(
    blogPost.id ? 'PUT' : 'POST',
    blogPost
  );

  return savedBlogPost;
}

Special Thanks

The people who are responsible for TSDX. A great utility to help you bootstrap a new npm library with TypeScript, Rollup and Jest testing framework. See TSDX on Github.

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.