Dictionarius: Thennable

The Womack Dictionarius word of the day is Thennable (sometimes spelled thenable).

Cases

There are two primary cases of this word. I’ve provided example sentences for each case:

Case 1 ~ Adjective

Is the return value of API.getDictionariusEntries() thennable? I want to try await with it

Case 2 ~ Noun

Each of the methods of the API object are Thennables. We feel this is a good convention for any async-heavy networking object.

Duck Reasons

You may ask “why do we need the word/concept thennable given we already have the word/concept Promise?”. I’m glad you asked madam. The reason we need “thennable” is, well, JavaScript (or ducks). JavaScript, like Ruby & Objective-C, makes heavy use of duck-typing. Despite typed-functional-language pundits manufacturing successive declarations of imminent conflagration vis-à-vis dynamically typed proglangs, dynamic typing that’s deferred to runtime (duck typing) has benefits in both unit testing and general programming. The Thennable is a supreme manifestation of said benefits. When a JavaScript function designed for a Promise receives a Thennable (often in a unit testing scenario), things Just Work™. I don’t know about you madam, but I don’t want to conform to a protocol just the unit test a simple function. Example:

1
2
3
4
5
// foo.js

export function promiseMe (prollyAPromiseRight, doSomethingElse) {
return prollyAPromiseRight.then(doSomethingElse)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// foo.test.js
import spec from 'tape'
import { spy } from 'sinon'
import { promiseMe } from './foo'

spec('foo', ({ test, end : endSpec }) => {
test('promiseMe', ({ ok, end : endTest }) => {
const thennable = { then : spy() }
const doSomethingElse = spy(function () {
ok(thennable.then.calledOnce, 'the object in the 1st position gets "thenned"')
ok(thennable.then.calledWith(doSomethingElse))
ok(doSomethingElse.calledOnce, '`then` is called with the fn in the 2nd position')
endSpec()
})

promiseMe(thennable, doSomethingElse)
})
endSpec()
})

Now I could have used a spied-on Promise in that test. But the experienced unit tester knows they should write the simplest, fastest test that will satisfy the requirements. A Thennable takes up less CPU cycles, less memory and more accurately reflects the requirements of the function than a Promise instance would.

May the “then” last forever!

Until next time in The Womack Dictionarius—keep on “thenning”.