0.1.1 • Published 3 years ago

promise-cache-sync v0.1.1

Weekly downloads
5
License
MIT
Repository
github
Last release
3 years ago

promise-cache-sync

Reuse your promises with synchronous operations.

Build Status Node.js CI

Overview

Promises in JavaScript

const prom = new Promise((res, rej) => {
    res(50);
});
console.log("1st Run");
console.log("Promise 1 Before");
prom.then(console.log);
console.log("Promise 1 After");
setTimeout(() => {
    console.log("2nd Run");
    console.log("Promise 2 Before");
    prom.then(console.log);
    console.log("Promise 2 After");
}, 50);

Output:

1st Run
Promise 1 Before
Promise 1 After
50
2nd Run
PromiseCache 2 Before
PromiseCache 2 After
50

New cached Promises in JavaScript

PromiseCache.set('mypath', (res, rej) => {
    res(500);
});
const prom = PromiseCache.get('mypath');
console.log("1st Run");
console.log("PromiseCache 1 Before");
prom.then(console.log);
console.log("PromiseCache 1 After");
setTimeout(() => {
    console.log("2nd Run");
    const prom2 = PromiseCache.get('mypath');
    console.log("PromiseCache 2 Before");
    prom2.then(console.log);
    console.log("PromiseCache 2 After");
}, 50);

Output:

1st Run
Promise 1 Before
Promise 1 After
50
2nd Run
PromiseCache 2 Before
50
PromiseCache 2 After

Installation

Using npm

npm install promise-cache-sync --save

Or directly on your browser, simply download your file from the following:

<script type="application/javascript" src="promise-cache-sync.js"></script>
<script type="application/javascript" src="promise-cache-sync.min.js"></script>

Usage

const { PromiseCache } = require("promise-cache-sync");
newProm = PromiseCache.set('mypath', (res, rej) => {
    res(500);
});
PromiseCache.get('mypath').then(shouldBe(500));
setTimeout(() => {
    PromiseCache.get('mypath').then(val => val * 2).then(val => val * 2).then(shouldBe(2000));
    PromiseCache.get('mypath').then(shouldBe(500));
}, 25);

All Features:

  • All Promise functions supported, like then(), catch(), finally().
  • Sync promise value for future events.

Help us expand

Let me know in issues/github page or on email which javascript functions to include in next release. Or help us writing the complex test cases for the project. Check all the Contributing authors to this library.