memorize

0.0.4 • Public • Published

Memorize

Like async's memoize. But without the rest.

Example

var memo = require('memorize');

var foo = memo(function(cb) {
  setTimeout() {
    // this function takes long, long, long
    // but's only going to run once.
    cb('Foo!')
  }, 5000);
})

foo(function(msg) {
  console.log(msg); // should take 5 secs

  foo(function(other_message) {
    console.log(other_message); // immediate
  })
})

You can also set an expiration time on memoization.

var foo = memo(function(cb) { 
  // magic happens here, but it takes long
}, 7000); // expire after 7 secs.

foo(function(res) {
  // took a while

  foo(function(res) {
    // immediate
  })

  setTimeout(function() {
    foo(function(res) {
      // expiration passed, so function is re-run
    })
  }, 10000);
})

That's it. For more info take a look at the examples.

About

By Tomás Pollak. (c) Fork, Ltd. MIT licensed.

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.0.4
    17
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 0.0.4
    17
  • 0.0.3
    3
  • 0.0.2
    0
  • 0.0.1
    6

Package Sidebar

Install

npm i memorize

Weekly Downloads

25

Version

0.0.4

License

MIT

Last publish

Collaborators

  • tomas