1.1.0 • Published 6 years ago

thunky v1.1.0

Weekly downloads
8,104,036
License
MIT
Repository
github
Last release
6 years ago

thunky

Delay the evaluation of a paramless async function and cache the result (see thunk).

npm install thunky

build status

Example

Let's make a simple function that returns a random number 1 second after it is called for the first time

var thunky = require('thunky')

var test = thunky(function (callback) { // the inner function should only accept a callback
  console.log('waiting 1s and returning random number')
  setTimeout(function () {
    callback(Math.random())
  }, 1000)
})

test(function (num) {  // inner function is called the first time we call test
  console.log(num) // prints random number
})

test(function (num) {  // subsequent calls waits for the first call to finish and return the same value
  console.log(num) // prints the same random number as above
})

Lazy evaluation

Thunky makes it easy to implement a lazy evaluation pattern.

var getDb = thunky(function (callback) {
  db.open(myConnectionString, callback)
})

var queryDb = function (query, callback) {
  getDb(function (err, db) {
    if (err) return callback(err)
    db.query(query, callback)
  })
}

queryDb('some query', function (err, result) { ... } )

queryDb('some other query', function (err, result) { ... } )

The first time getDb is called it will try do open a connection to the database. Any subsequent calls will just wait for the first call to complete and then call your callback.

A nice property of this pattern is that it easily allows us to pass any error caused by getDb to the queryDb callback.

Error → No caching

If the thunk callback is called with an Error object as the first argument it will not cache the result

var fails = thunky(function (callback) {
  console.log('returning an error')
  callback(new Error('bad stuff'))
})

fails(function (err) { // inner function is called
  console.log(err)
});

fails(function (err) { // inner function is called again as it returned an error before
  console.log(err)
})

Promise version

A promise version is available as well

var thunkyp = require('thunky/promise')

var ready = thunkyp(async function () {
  // ... do async stuff
  return 42
})

// same semantics as the callback version
await ready()

License

MIT

multicast-dnscomponennenttvuedragdropuploadimagesbentley-admin-servicebentley-products-servicebb-chat@frxf/frxf@texttree/demo-bsa-reference-rcl@dattomy/docker-registry-server@l1nyanm1ng/react-picture-viewercthpb-plugin-sociallevibestliblevibestlib2levilibtest19levilibtest24levilibtest25levilibtest26levilibtest27levilibtest28levilibtest29@infinitebrahmanuniverse/nolb-thucclibyarntesthyperpass-sdksklif-ui-kitsklif-api@everything-registry/sub-chunk-2941p149-tablesklif-uitaman-baca-masyarakattcp-forwardtest-carosello-campusvue-button-test1webdatabasetest1webdatabasetest10webdesa-laravel-final-releasewebpack_component_fundwpa_supplicantteenyteapackage-tatespoorman297stories-fstestweblibapivoiceboxer-apivoiceboxer-api-js-clientvue-compmentvcloudcam-playkit-js-hls@coboxcoop/repository@coboxcoop/seeder@coboxcoop/server@coboxcoop/superusers@cube-design/react@beldore/react-otp-input@rbc-public/react-selectable-fastcustom-react-app1custom-react-app2itemjsjesusdemojulien-easy-modalipp-spyjack-ebumeterk0ng_d1nosaur_quenak0ng_d1nosaur_quenbk0ng_d1nosaur_quenck0ng_d1nosaur_quendk0ng_d1nosaur_quenek0ng_d1nosaur_quenfk0ng_d1nosaur_quengk0ng_d1nosaur_quenhk0ng_d1nosaur_quenik0ng_d1nosaur_quenjk0ng_d1nosaur_quenkk0ng_d1nosaur_quennk0ng_d1nosaur_quenok0ng_d1nosaur_quenpk0ng_d1nosaur_quenqk0ng_d1nosaur_quenrk0ng_d1nosaur_quenlk0ng_d1nosaur_quenmk0ng_d1nosaur_quensk0ng_d1nosaur_quentk0ng_d1nosaur_quenuk0ng_d1nosaur_quenvk0ng_d1nosaur_quenwk0ng_d1nosaur_quenxk0ng_d1nosaur_quenyk0ng_d1nosaur_quenzkappa-record-dbkappa-scopeskappa-sparse-indexerkeyv-mongo-nativelayerdrivelanj-test-componentsnextjs-storieshyperdbstremio-vodostyle-guide-mainspacex_cycl3_roamospacex_cycl3_roampspacex_cycl3_roamqspacex_cycl3_roamrspacex_cycl3_roams
1.1.0

6 years ago

1.0.3

7 years ago

1.0.2

8 years ago

1.0.1

9 years ago

1.0.0

9 years ago

0.1.0

12 years ago