Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
How can I stop a loop from completing once it gets the first returned object from an API?

The code below receives all of the user's redeemed rewards from an API, I need to stop the loop from completing them all at once.

The for loop runs through all of the rewards the current user has redeemed through Twitch's API, then fulfills them if specific conditions are met. I want it to only fulfill one redemption, not all (x) amount of them.

The fulfill reward part happens at: fulfillReward()

For a full code snippet, click here: https://pastebin.com/7k5WNhmD

        // looping over reward returned data
    for (let i = 0; i < rewards.length; i++) {
      async function fulfillReward() {
        await fetch(
          `https://api.twitch.tv/helix/channel_points/custom_rewards/redemptions?broadcaster_id=58606718&reward_id=08d5e2d9-ddd7-4082-bc78-39b06b35cd68&id=${rewards[i].id}`,
          {
            method: 'PATCH',
            headers: {
              'client-Id': process.env.TWITCHBOT_CLIENT_ID,
              Authorization: `Bearer ${process.env.TWITCHBOT_ACCESS_TOKEN}`,
              'Content-Type': 'application/json',
            },
            body: JSON.stringify({
              status: 'FULFILLED',
            }),
          }
        )
      }

      const currentReward = rewards[i]
      const currentRewardUsername = currentReward.user_name.toLowerCase()

      if (currentRewardUsername === input.toLowerCase()) {
        countDocuments(discordID)
          .then(() => {
            return findOneUser(discordID)
          })
          .then(() => {
            // All (x) amount of rewards get fulfilled instead of the first matching result 
            fulfillReward()
            interaction.reply('success')
          })
          .catch((err) => console.log(err))
      } else if (currentRewardUsername != input.toLowerCase()) {
        return interaction.reply(`The Twitch user **${input}** has not redeemed the channel reward!`)
      }
    }
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

The general solution to exit a loop when a condition is met, is to incorporate a break statement in whatever conditional decides the loop has achieved its purpose early.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/break

I've made a simple snippet that runs a million cycle for-next loop but terminates after 10 loops when a condition is met, using break.

let loopNumber = 0;

for (let i=0; i<1000000; i++)
  {
    loopNumber++;
    
    if (loopNumber==10) {
    break;
    }
    
  } // next i;
  
  console.log(loopNumber);

about 4 years ago · Juan Pablo Isaza Report

0

Not sure I understood completely what you actually want but lets try:

   async function fulfillReward(reward) {
      await fetch(
        `https://api.twitch.tv/helix/channel_points/custom_rewards/redemptions?broadcaster_id=58606718&reward_id=08d5e2d9-ddd7-4082-bc78-39b06b35cd68&id=${reward.id}`,
        {
          method: 'PATCH',
          headers: {
            'client-Id': process.env.TWITCHBOT_CLIENT_ID,
            Authorization: `Bearer ${process.env.TWITCHBOT_ACCESS_TOKEN}`,
            'Content-Type': 'application/json',
          },
          body: JSON.stringify({
            status: 'FULFILLED',
          }),
        }
      )     
    }

If you just receive the reward as a parameter you don't need to declare functions inside the for (please don't ever do that)

Then you call it sending the reward parameter

 if (currentRewardUsername === input.toLowerCase()) {
        countDocuments(discordID)
          .then(() => {
            return findOneUser(discordID)
          })
          .then(() => {
            fulfillReward(currentReward)
            interaction.reply('success')
          })
          .catch((err) => console.log(err))
      } else if (currentRewardUsername != input.toLowerCase()) {
        return interaction.reply(`The Twitch user **${input}** has not redeemed the channel reward!`)
      }

I updated your code here https://pastebin.com/clone/7k5WNhmD, but cant really test it, so please let me know if you need something else

about 4 years ago · Juan Pablo Isaza Report

0

try this.

for (let i = 0; i < rewards.length; i++) {
      async function fulfillReward() {
        await fetch(
          `https://api.twitch.tv/helix/channel_points/custom_rewards/redemptions?broadcaster_id=58606718&reward_id=08d5e2d9-ddd7-4082-bc78-39b06b35cd68&id=${rewards[i].id}`,
          {
            method: 'PATCH',
            headers: {
              'client-Id': process.env.TWITCHBOT_CLIENT_ID,
              Authorization: `Bearer ${process.env.TWITCHBOT_ACCESS_TOKEN}`,
              'Content-Type': 'application/json',
            },
            body: JSON.stringify({
              status: 'FULFILLED',
            }),
          }
        )
      }

      const currentReward = rewards[i]
      const currentRewardUsername = currentReward.user_name.toLowerCase()
      

      if (currentRewardUsername === input.toLowerCase()) {
         function doSomething() {
              return new Promise((resolve, reject) => {
                 countDocuments(discordID)
                  .then(() => {
                    return findOneUser(discordID)
                  })
                  .then(() => {
                    // All (x) amount of rewards get fulfilled instead of the first matching result 
                    fulfillReward()
                    interaction.reply('success')
                    resolve(true);
                  })
                  .catch((err) => console.log(err))
            })
         }
          
         const ret = await doSomething();
 
         if (ret)
            return ;
            
      } else if (currentRewardUsername != input.toLowerCase()) {
        return interaction.reply(`The Twitch user **${input}** has not redeemed the channel reward!`)
      }
    }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!