react-native-in-app-utils icon indicating copy to clipboard operation
react-native-in-app-utils copied to clipboard

Add promise support

Open sibelius opened this issue 8 years ago • 8 comments

Promises are much easier to work with instead of callbacks, and we can use inside async functions using await making programming easier

sibelius avatar Jun 19 '16 23:06 sibelius

In relation to this, how would I be able to use put() from redux-saga inside the callback?

For example saving the products into the store The ff code doesn't work and I can't use yield inside:

export function* iosSubscriptionSaga() {
InAppUtils.loadProducts(products, (error, products) => {
        put(IapActions.loadSubscriptionProduct(products[0]));
 });
}

I know I must be missing something. Any help would be appreciated.

annelorraineuy avatar Jan 09 '17 22:01 annelorraineuy

If you do not want to wait for promises support, you may use bluebird.promisifyAll. Example:

bluebird.promisifyAll(InAppUtils); const products = await InAppUtils.loadProductsAsync(...); // instead of just loadProducts

kulyk avatar Aug 04 '17 13:08 kulyk

@antonkulyk Are you currently using bluebird with InAppUtils in production? This is a great recommendation and I'm really considering it.

kcfgl avatar Aug 09 '17 18:08 kcfgl

@kcfgl not in production yet, but I've been using bluebird for a long time in different projects and I had no trouble with it, so I believe it's stable enough

kulyk avatar Aug 10 '17 08:08 kulyk

Oh @antonkulyk !!! you are GOD !!!

zoi-aoba avatar Oct 09 '18 14:10 zoi-aoba

@antonkulyk thank you!

vitalyliber avatar Apr 08 '19 19:04 vitalyliber

@vitalyliber hi, looking back at that issue, now I would pick another approach and promisify everything on my own. See the example below:

class PromisifiedInAppUtils {
   static loadProducts(products) {
      return new Promise((resolve, reject) => {
         InAppUtils.loadProducts(products, (error, loadedProducts) => {
            if (error) {
               return reject(error)
            }
            resolve(loadedProducts)
         });
      })
   }
}

...

try {
   // Use with async functions as now it returns a promise
   const loaded = await PromisifiedInAppUtils.loadProducts(products)
} catch (error) {
   // Handle exceptions with try / catch as we love ❤️
}

// If you use redux-saga, you can use it like that
const loaded = yield call(PromisifiedInAppUtils.loadProducts, products)

Benefits:

  1. No extra dependencies, fewer things you don't control, fewer risks
  2. The dependency from react-native-in-app-utils is hidden inside your own class and you can later easily switch the library without changing the code using your class. More about that

kulyk avatar Apr 09 '19 12:04 kulyk

@vitalyliber if you're just starting to integrate in-app purchases in your app, I'd recommend using another library. This one has been updated in February, looks abandoned, which is a huge risk in the React Native world. Take a look at the react-native-iap. They have an Android implementation as well

kulyk avatar Apr 09 '19 12:04 kulyk