Closed KooiRUG closed 4 years ago
That code works like an alternative implementation of a promise. For example await
will call that .then
function.
I would suggest informing yourself on how promises work. They are not as suffisticated as you might think. What you implemented is actually a promise-like function as nicolo said. It works as an alternative implementation to the browser/node implementation of a promise. As you might already know, there are many of these implementations e.g. Bluebird. This function "catches" them all.
There is something to be said, however, on why this would need a package of it's own...
There is also the possibility to write code in a style that can handle both scenarios (by always treating it as a promise) - so you don't have to check it
async function log (something_unknown) {
console.log(await something_unknown)
}
// or
function log (something_unknown) {
return Promise.resolve(something_unknown).then(console.log)
}
promise = log(Promise.resolve(5)) // prints 5
promise = log(4) // prints 4
So yea, maybe you don't need this package
Please seriously consider what you're doing before you post in this tone. This question has been asked and answered many times in the issue tracker for is-promise, so search those if you want a full answer, but the short answer is that:
.then
method to be treated as a Promise.I hope I've answered your question, but more importantly I hope @KooiRUG and @Miladiir will think more about the impact of their comments in future. Comments like these are a massive drain on the time and energy of open source maintainers, the questions you raise are already answered hundreds of times over, and they contribute absolutely nothing to society.
I don't think this is a very useful snippet, sorry.
console.log(isPromise({then: () => {}}));