Class Listing<T>

A Listing of items.

Listings are probably the most common data type on Reddit. Snoots intentionally exposes as little as possible about the internal workings to minimize the amount of boilerplate needed to interact with them.

Note

Since Reddit's responses are paged, Listings only implement asyncIterator. This means that if you want to loop over it you have to use for await:

const posts = await client.subreddits.getNewPosts("funny");
for await (const post of posts) {
console.log(post.title);
}

Note

If you want to iterate using callbacks, you can use forEach:

const posts = await client.subreddits.getNewPosts("funny");
await posts.forEach(post => console.log(post.title));

Note

If you want to get an entire page at a time, use eachPage:

const posts = await client.subreddits.getNewPosts("funny");
await posts.eachPage(page => console.log(page.length));

Type Parameters

  • T

    The type of items this Listing holds.

Methods

  • Whether or not this listing can perform a fetch to get more data.

    If this returns false you can be sure that this listing will never cause an api call. If this is true it does not mean that there are more unfetched items in the Listing, only that there might be.

    Returns boolean

    true if the listing could try to fetch more items, false otherwise.

  • Execute a function on pages of the listing.

    Parameters

    • handler: AwaitableFunction<T[], boolean | void>

      The function to execute on each page. If this returns or resolves to false the execution will be halted.

    Returns Promise<void>

    A promise that resolves when the listing has been exhausted.

  • Whether or not this listing is empty.

    Returns Promise<boolean>

    A promise that resolves to either true if the listing is empty or false if it's not.

  • Get the first item of this listing.

    Returns Promise<Maybe<T>>

    A promise that resolves to either the first item of the listing, or undefined if the listing is empty.

  • Execute a function on each element of the listing.

    Parameters

    • handler: AwaitableFunction<T, boolean | void>

      The function to execute on each item in the listing. If this returns or resolves to false the execution will be halted.

    Returns Promise<void>

    A promise that resolves when the iteration is complete.

    Note

    This is an enhanced version of the default Array.forEach. It allows for asynchronous callbacks and breaking.

    Example: Async

    async function slowAsync(post: Post): Promise<void> {
    // do something slow
    }

    const posts = await client.subreddits.getNewPosts("funny");
    await posts.forEach(post => slowAsync(post));

    Example: Breaking

    const posts = await client.subreddits.getNewPosts("funny");
    await posts.forEach(post => {
    console.log(post.title);
    // Break if the post was more than 5 minutes old.
    return post.createdUtc >= (Date.now() / 1000) - 5 * 60;
    });
  • Determines whether the specified callback function returns true for any element of the listing.

    Parameters

    • handler: AwaitableFunction<T, boolean>

      The matcher to run on each element in the listing. If this returns true at any point the searching is stopped.

    Returns Promise<boolean>

    A promise that resolves to true if handler returned true for some element in the listing, or false if it reached the end of the listing without finding a match.

Generated using TypeDoc