Giter Club home page Giter Club logo

guard-fn's Introduction

@devteks/guard-fn

  • callOnce: Wrapper for a function to be called only once

  • guarded: execute function guarded with pre function and post function

  • guardedAsync: execute async function guarded with pre function and post function

  • guard: guards function with pre function and post function and return same function signature

  • guardAsync: guards async function with pre function and post function and return same function signature

how to use

npm install @devteks/guard-fn --save

Usage:

import:

const { callOnce, guarded, guardedAsync, guard, guardAsync } = require('@devteks/guard-fn');
// OR
import { callOnce, guarded, guard } from '@devteks/guard-fn';

Using callOnce

  const { callOnce } = require('@devteks/guard-fn');
  const fn = callOnce((a, b) => {
    console.log('called with:', a, b);
    return a + b;
  });
  console.log(fn(1, 2));
  // prints `called with: 1 2` and `3`
  fn(1, 2); // 3
  // prints `3`
  fn(1, 2); // 3
  // prints `3`

Using guarded

const { guardedAsync } = require('@devteks/guard-fn');

(async () => {
    const result = await guardedAsync(
      async () => {
        await delay(1000);
        return 10;
      },
      () => console.log("before run"),
      () => console.log("after run")
    );
    
    console.log("result:", result);
    // prints after delay for 1 second
    // before run
    // after run
    // result: 10
})();

Using guard

(async () => {
const { guardAsync } = require('@devteks/guard-fn');
  const fn = guardAsync(
    async (a: number, b: number) => {
      await delay(10);
      return a + b;
    },
    () => console.log("before run"),
    () => console.log("after run")
  );

  const result = await fn(1, 2);
  console.log("result:", result);
})();

guard-fn's People

Contributors

mosamuhana avatar

Watchers

 avatar James Cloos 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.