question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Type error with async/await

See original GitHub issue

I updated to version 1.19.0 of the package (which includes typings). I get the following error when trying to await promises (Typescript):

Type is referenced directly or indirectly in the fulfillment callback of its own 'then' method

Here are the snippets that receive the error:

const user = await reddit.getUser(query.trim()).fetch() //param is string
const post = await reddit.getRandomSubmission()
const post = await reddit.getSubmission(postIDS[i]).fetch() //param is string
const posts = await reddit.getSubreddit(subreddit).getRandomSubmission() //param is string

This error seems to come from the package. While I don’t see a mention of async/await in the docs, it did work perfectly before…

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:20
  • Comments:12

github_iconTop GitHub Comments

6reactions
Loeffeldudecommented, Jun 10, 2022

A better quick fix is putting this in a ts file in your project. This way you don’t need to cast or use a function. This then fixes it globally.

import * as snoowrap from "snoowrap";

declare module "snoowrap" {
  class RedditContent<T> {
    then: undefined;
    catch: undefined;
    finally: undefined;
  }
}

3reactions
Tyrrrzcommented, Mar 24, 2022

Another hack/workaround for this is to use Omit<> type in TypeScript:

const unpromise = async <T>(promise: Promise<T>) => {
  const result = await promise;
  return result as Omit<T, 'then' | 'catch' | 'finally'>;
};

// This produces an error
const comment = await reddit.getComment('...');

// This does not produce an error
const comment = await unpromise(reddit.getComment('...'));
Read more comments on GitHub >

github_iconTop Results From Across the Web

Error handling with Async/Await in JS | by Ian Segers | ITNEXT
Learn error handling in JS with sync and async code and some caveats. ... now create a new error of type TypeError and...
Read more >
Async/Await Error Handling - Beginner JavaScript - Wes Bos
We will talk about error handling strategies for async await in this lesson. Because there is no .then() that we are chaining on...
Read more >
Error handling with async/await and promises, n² ... - CatchJS
When dealing with promises, you have no way of knowing if an error will be handled some time in the future. The promise...
Read more >
Async/await - The Modern JavaScript Tutorial
If a promise resolves normally, then await promise returns the result. But in the case of a rejection, it throws the error, just...
Read more >
Better error handling with async/await - DEV Community ‍ ‍
Async /await is a special syntax to work with promises in a more concise way. Adding async before any function turns the function...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found