node-osmosis icon indicating copy to clipboard operation
node-osmosis copied to clipboard

Use real "thenable" promises

Open nashwaan opened this issue 7 years ago • 5 comments

In the list of features, there is "Clean promise-like interface".

Can you show a simple example how to use this. Are there resolve() and then() functions?

nashwaan avatar Jul 21 '16 06:07 nashwaan

I'm also struggling with the promise like interface. You can find a few examples right here: https://github.com/rchipka/node-osmosis/wiki#then

The main problem is to integrate it in other promise chains because of the then function interface.

alexander-heimbuch avatar Aug 28 '16 07:08 alexander-heimbuch

@alexander-heimbuch Thanks for pointing to this wiki https://github.com/rchipka/node-osmosis/wiki#then. Although i use osmosis extensively, and refer to this http://rchipka.github.io/node-osmosis/Command.html#then__anchor documentation thinking this is the only doc available on osmosis. However, I never knew there is more info in the wiki page.

A big thank you.

nashwaan avatar Aug 28 '16 09:08 nashwaan

You are welcome. I'm working around osmosis with Bluebird wrapping the osmosis call in a promise. Would be much more elegant if the then function from osmosis would be a real promise so I could simply pass it through the promise chain.

import Bluebird from 'bluebird'
import osmosis from 'osmosis'

export default () =>
  new Bluebird((resolve, reject) => {
    osmosis.get(url)
      .set({
        foo: '.bar',
        bar: '#baz'
      })
      .data((listing) => resolve({
        foo: listing.foo.trim(),
        bar: listing.bar.trim().replace(/\[.+?\]\s*/g, '')
      }))
  })

alexander-heimbuch avatar Aug 28 '16 10:08 alexander-heimbuch

@alexander-heimbuch this is what i do as well!

I am satisfied with using bluebird as the solution and can't complain. osmosis simple and powerful API and being free is too good to complain about lack of a thenable promise.

nashwaan avatar Aug 29 '16 10:08 nashwaan

I would like to avoid using real promises throughout because it would mean extending each Promise instance (or Promise.prototype) with Osmosis commands.

My thought is that .then() could support passing in a promise. Additionally, if the user gives .done a promise, then Osmosis could return that promise instead of the Osmosis instance, which would also allow the use of await.

rchipka avatar Oct 02 '17 16:10 rchipka