Skip to content

Instantly share code, notes, and snippets.

@rauschma
Created April 10, 2016 20:33
Show Gist options
  • Save rauschma/2193594f72a1e92bbacd7747ce8624f9 to your computer and use it in GitHub Desktop.
Save rauschma/2193594f72a1e92bbacd7747ce8624f9 to your computer and use it in GitHub Desktop.
// Demo: converting a thenable to a Promise
const fulfilledThenable = {
then(reaction) {
reaction('hello');
}
};
const promise = Promise.resolve(fulfilledThenable);
console.log(promise instanceof Promise); // true
promise.then(x => console.log(x)); // hello
@gunar
Copy link

gunar commented Apr 11, 2016

Regarding thenables, @bahmutov teached me to start with a promise so you don't have to wrap all library calls with Promise.resolve().

Promise.resolve()
  .then(thenableReturningFn)

instead of

Promise.resolve(thenableReturningFunction())
  .then(...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment