thunky

WebJar for thunky

Лицензия

Лицензия

MIT
Группа

Группа

org.webjars.npm
Идентификатор

Идентификатор

thunky
Последняя версия

Последняя версия

1.1.0
Дата

Дата

Тип

Тип

jar
Описание

Описание

thunky
WebJar for thunky
Ссылка на сайт

Ссылка на сайт

https://www.webjars.org
Система контроля версий

Система контроля версий

https://github.com/mafintosh/thunky

Скачать thunky

Как подключить последнюю версию

<!-- https://jarcasting.com/artifacts/org.webjars.npm/thunky/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>thunky</artifactId>
    <version>1.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/thunky/
implementation 'org.webjars.npm:thunky:1.1.0'
// https://jarcasting.com/artifacts/org.webjars.npm/thunky/
implementation ("org.webjars.npm:thunky:1.1.0")
'org.webjars.npm:thunky:jar:1.1.0'
<dependency org="org.webjars.npm" name="thunky" rev="1.1.0">
  <artifact name="thunky" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='thunky', version='1.1.0')
)
libraryDependencies += "org.webjars.npm" % "thunky" % "1.1.0"
[org.webjars.npm/thunky "1.1.0"]

Зависимости

Библиотека не имеет зависимостей. Это самодостаточное приложение, которое не зависит ни от каких других библиотек.

Модули Проекта

Данный проект не имеет модулей.

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

Версии библиотеки

Версия
1.1.0
1.0.3
1.0.2