react-native-in-app-utils
react-native-in-app-utils copied to clipboard
Add promise support
Promises are much easier to work with instead of callbacks, and we can use inside async functions using await
making programming easier
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.
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
@antonkulyk Are you currently using bluebird with InAppUtils in production? This is a great recommendation and I'm really considering it.
@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
Oh @antonkulyk !!! you are GOD !!!
@antonkulyk thank you!
@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:
- No extra dependencies, fewer things you don't control, fewer risks
- 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
@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