chirag04 / react-native-in-app-utils

A react-native wrapper for handling in-app payments
MIT License
891 stars 185 forks source link

Add promise support #28

Open sibelius opened 8 years ago

sibelius commented 8 years ago

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

annelorraineuy commented 7 years ago

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.

kulyk commented 7 years ago

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

kcfgl commented 7 years ago

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

kulyk commented 7 years ago

@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

zoi-aoba commented 6 years ago

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

vitalyliber commented 5 years ago

@antonkulyk thank you!

kulyk commented 5 years ago

@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 commented 5 years ago

@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