Socket
Book a DemoInstallSign in
Socket

cache-for-promise

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cache-for-promise

simple cache for promise-returning-functions.

1.0.2
latest
Source
npmnpm
Version published
Weekly downloads
0
-100%
Maintainers
1
Weekly downloads
 
Created
Source

cache-for-promise

simple cache for promise-returning-functions.

Install

npm install cache-for-promise

Usage

basic usage

import CacheForPromise from "cache-for-promise"

let anyPromiseFunction = (arg)=>{
    console.log("any promise function is called");
    return new Promise((resolve,reject)=>{
        return resolve(arg);
    })
}

const ttl = 10;//seconds
const cfp = new CacheForPromise(anyPromiseFunction,ttl);

let arg = 1;
cfp.exec("cache-key-1",arg).then(result=>{
    console.log(result);//1
})
//"any promise function is called" is output


cfp.exec("cache-key-1",arg).then(result=>{
    console.log(result);//1
})
//key is same and cache is not expired, so "any promise function is called" is not output.

i.e with axios

import CacheForPromise from "cache-for-promise"
import axios from "axios"

const ttl = 10;//seconds
const cfp = new CacheForPromise(axios,ttl);

const axiosArguments = {
    method:"get",
    params:{test1:1,test2:2},
    url:"/any-url"
};//recommended for only get.

cfp.exec("cache-key-1",axiosArguments).then(result=>{
    console.log(result.data);
}).catch(error=>{
    console.log(error);
})

cfp.exec("cache-key-1",axiosArguments).then(result=>{
    console.log(result.data);
}).catch(error=>{
    console.log(error);
})


with auto cache key functions

import CacheForPromise from "cache-for-promise"
import KeyGenerator from "cache-for-promise/key-generator"
import axios from "axios"

const ttl = 10;//seconds
const cfp = new CacheForPromise(axios,ttl);

cfp.keygen = KeyGenerator.axios;//only for axios is prepared, now.

const axiosArguments = {
    method:"get",
    params:{test1:1,test2:2},
    url:"/any-url"
};//recommended for only get.

cfp.exec(axiosArguments).then(result=>{
    console.log(result.data);
}).catch(error=>{
    console.log(error);
})

cfp.exec(axiosArguments).then(result=>{
    console.log(result.data);
}).catch(error=>{
    console.log(error);
})

Keywords

cache

FAQs

Package last updated on 25 Jun 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.