title: Graph API: What is the correct way to interrupt Pagination
body: <p>I am using this script to fetch Chats. I need 100 chats maximum but it may happen that a chat do not have 100 messages. How can I handle that case in this script?</p>
  <p>I am using Node Package Microsoft Graph Client.</p>
  <pre class="lang-js prettyprint-override"><code>    const { Client, PageIterator } = require('@microsoft/microsoft-graph-client');
      async getChatList(GroupChatId) {
          let messages = [];
  
          let count = 0;
          let pauseAfter = 100; // 100 messages limit
  
          let response = await this.graphClient
              .api(`/chats/${GroupChatId}/messages`)
              .version('beta')
              .get();
  
  
          let callback = (data) =&gt; {
              messages.push(data);
              count++;
              return count &lt; pauseAfter;
          }
  
          let pageIterator = new PageIterator(this.graphClient, response, callback);
          await pageIterator.iterate();
  
          return messages;
  
      }
  </code></pre>