raineorshine / p-memoize

Memoize promise-returning & async functions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

p-memoize Build Status

Memoize promise-returning & async functions

Useful for speeding up consecutive function calls by caching the result of calls with identical input.

Install

$ npm install p-memoize

Usage

const pMemoize = require('p-memoize');
const got = require('got');

const memGot = pMemoize(got, {maxAge: 1000});

(async () => {
	memGot('sindresorhus.com');

	// This call is cached
	memGot('sindresorhus.com');

	setTimeout(() => {
		// This call is not cached as the cache has expired
		memGot('sindresorhus.com');
	}, 2000);
})();

API

pMemoize(fn, [options])

Returns a memoized version of the fn function.

fn

Type: Function

Promise-returning or async function to be memoized.

options

Type: Object

See the mem options.

pMemoize.clear(memoized)

Clear all cached data of a memoized function.

Will throw if passed a non-memoized function.

Related

License

MIT © Sindre Sorhus

About

Memoize promise-returning & async functions

License:MIT License


Languages

Language:JavaScript 84.8%Language:TypeScript 15.2%