Sam
Sam

Reputation: 2331

React Native: RNIap.getPurchaseHistory().then runs infinately

The project is at this Github Repository. The file with the code is at components/Soundboard.js


This code was working previously, but now it looks like the promise is running forever. It looks like neither the resolve function, nor the reject function are executing because if I uncomment all the commented lines below and call the function askForPurchase() the only things printed to the console are

The buyProduct() function also is no longer initializing an IAP.

const buyProduct = function(){
    RNIap.requestPurchase("1985162691", false).then(purchase => {
        store.dispatch(setPurchases(purchase))
        await RNIap.finishTransaction(purchase, false)    //developerPayloadAndroid?: string   Should I use this argument? I don't get how to use it
    }).catch((error) => {
        console.log(error.message);
    })
}

const askForPurchase = function(){
    if (!store.getState().purchase){
        //console.log(RNIap.getPurchaseHistory())
        RNIap.getPurchaseHistory().then(purchase => {
            //console.log(`test1`)
            store.dispatch(setPurchases(purchase))
            if (purchase.length == 0){
                //console.log(`test if`)
                buyProduct()
            }else{
                //console.log(`test else`)
                RNIap.getAvailablePurchases()
            }
        }, reason => {
            console.log(reason)
        })
        //console.log(`end`)
    }
}

EXTRA

This code was working a few months ago and I even pulled a commit(1b9cb81f229680e173ce910892dddedc632c1651, comment: "Made the seal pic more cartoony") from that time to test out. After pulling this commit, I deleted my node_modules and pods, and cleaned my build folder, but the askForPurchase() and buyProduct() functions no longer work in that commit either.

I am testing this on a real iPhone SE running ios 13.6.1


I created a sandbox tester if you need to test it out, but I don't think you'll need it

email: [email protected]
pw: Somepassword1

Upvotes: 1

Views: 1038

Answers (2)

Mayank Pandav
Mayank Pandav

Reputation: 455

hello @Sam problem is async await problem they are not able to get value because they are not waiting to get data before getting data its firing without data and it was returning promise so you have to use async function

so your code be like

const buyProduct = async()=>{
   await RNIap.requestPurchase("1985162691", false).then(purchase => {
    store.dispatch(setPurchases(purchase))
    await RNIap.finishTransaction(purchase, false)    //developerPayloadAndroid?: string   Should I use this argument? I don't get how to use it
}).catch((error) => {
    console.log(error.message);
})}

const askForPurchase = async()=>{
if (!store.getState().purchase){
    //console.log(await RNIap.getPurchaseHistory())
   await RNIap.getPurchaseHistory().then(purchase => {
        //console.log(`test1`)
        store.dispatch(setPurchases(purchase))
        if (purchase.length == 0){
            //console.log(`test if`)
            buyProduct()
        }else{
            //console.log(`test else`)
            RNIap.getAvailablePurchases()
        }
    }, reason => {
        console.log(reason)
    })
    //console.log(`end`)
}}

Upvotes: 1

Shing Ho Tan
Shing Ho Tan

Reputation: 951

You will need to change from

console.log(RNIap.getPurchaseHistory())

to

console.log(await RNIap.getPurchaseHistory())

Upvotes: 0

Related Questions