Gathering detailed insights and metrics for thunky
Gathering detailed insights and metrics for thunky
Gathering detailed insights and metrics for thunky
Gathering detailed insights and metrics for thunky
Delay the evaluation of a paramless async function and cache the result
npm install thunky
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
97 Stars
20 Commits
10 Forks
5 Watchers
1 Branches
2 Contributors
Updated on Oct 27, 2024
Latest Version
1.1.0
Package Id
thunky@1.1.0
Size
3.04 kB
NPM Version
6.9.0
Node Version
10.16.3
Published on
Oct 14, 2019
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Delay the evaluation of a paramless async function and cache the result (see thunk).
npm install thunky
Let's make a simple function that returns a random number 1 second after it is called for the first time
1var thunky = require('thunky') 2 3var test = thunky(function (callback) { // the inner function should only accept a callback 4 console.log('waiting 1s and returning random number') 5 setTimeout(function () { 6 callback(Math.random()) 7 }, 1000) 8}) 9 10test(function (num) { // inner function is called the first time we call test 11 console.log(num) // prints random number 12}) 13 14test(function (num) { // subsequent calls waits for the first call to finish and return the same value 15 console.log(num) // prints the same random number as above 16})
Thunky makes it easy to implement a lazy evaluation pattern.
1var getDb = thunky(function (callback) {
2 db.open(myConnectionString, callback)
3})
4
5var queryDb = function (query, callback) {
6 getDb(function (err, db) {
7 if (err) return callback(err)
8 db.query(query, callback)
9 })
10}
11
12queryDb('some query', function (err, result) { ... } )
13
14queryDb('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.
If the thunk callback is called with an Error
object as the first argument it will not cache the result
1var fails = thunky(function (callback) { 2 console.log('returning an error') 3 callback(new Error('bad stuff')) 4}) 5 6fails(function (err) { // inner function is called 7 console.log(err) 8}); 9 10fails(function (err) { // inner function is called again as it returned an error before 11 console.log(err) 12})
A promise version is available as well
1var thunkyp = require('thunky/promise') 2 3var ready = thunkyp(async function () { 4 // ... do async stuff 5 return 42 6}) 7 8// same semantics as the callback version 9await ready()
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
security policy file detected
Details
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 1/20 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More