This utility function makes sure that any value (Promise or otherwise)
will be turned into an object with a then property, so that we can write the same code
regardless of whether the input is sync or async, but guaranteeing that
if the input is sync, all operations will also be called synchronously.
The original input object is available under the thenable(...).src property.
After the then callback has finished, the Object's 'value' property will be set
to the 'resolved' value.
Example
// the same code can be applied without changes to a promise or a non promise // by doing it all in the then-callback thenable(123) .then( (v) => { console.log(v); returngetSomeOtherSyncOrAsyncVal(v); } ) .then( (otherVal) => { console.log(otherVal); returngetYetAnotherVal(v); } )
???
MAYBE a better solution would be to have a function called doAfter(value, (value) => { your code })
that checks whether it is a promise or not, and returns the result of the handler?
But without the pipe operator it would be a pain to chain them, unless it will return an object
with some properties like { result, doAfter:... }
or maybe thenable should always return a new object with properties { src, then, finally, ... } so
that the interface resembles a promise, but if we need the actual promise or value
we should simply call src?
Returns
an object that has a then function and a src property pointing to the original input
regardless whether it is a Promise or not
(Word play on then-able an th-enable)
This utility function makes sure that any value (Promise or otherwise) will be turned into an object with a then property, so that we can write the same code regardless of whether the input is sync or async, but guaranteeing that if the input is sync, all operations will also be called synchronously.
The original input object is available under the thenable(...).src property.
After the then callback has finished, the Object's 'value' property will be set to the 'resolved' value.
Example
??? MAYBE a better solution would be to have a function called
doAfter(value, (value) => { your code })
that checks whether it is a promise or not, and returns the result of the handler? But without the pipe operator it would be a pain to chain them, unless it will return an object with some properties like{ result, doAfter:... }
or maybe thenable should always return a new object with properties{ src, then, finally, ... }
so that the interface resembles a promise, but if we need the actual promise or value we should simply call src?Returns
an object that has a then function and a src property pointing to the original input regardless whether it is a Promise or not