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.

Return values of Promises are not checked in runtime

See original GitHub issue

This is a:

  • Bug Report

Which concerns (not really sure):

  • flow-runtime
  • babel-plugin-flow-runtime
  • flow-runtime-validators

The following code does not raise any errors:

type Person = {
  name: string
}

function untrustedEndpoint(id: string): Promise<{ data: Person }> {
  return Promise.resolve(null) // <= wrong
}

untrustedEndpoint('id')

Currently we will only get the type assertion error once another function receives the return value as an argument, so a workaround is to:

type Person = {
  name: string
}

type Endpoint = { data: Person }

function untrustedEndpoint(id: string): Promise<Endpoint> {
  return Promise.resolve(null) // <= wrong
}

function processPersonNameFromEndpoint(response : Endpoint) {
  return response.data.name
}

untrustedEndpoint('id').then(processPersonNameFromEndpoint)

Raised by @gcanti at https://github.com/gcanti/io-ts/issues/4#issuecomment-285756198

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
phpnodecommented, Mar 11, 2017

We can also skip this check in almost all cases in async functions - we’d only need it in the fairly rare case of returning a promise, ok, will add it.

0reactions
phpnodecommented, Mar 12, 2017

Not sure if returning a promise really is such a rare case (sometimes it’s superior than using async/await), but I understand where you’re coming from. Thanks!

I just mean in async function specifically:

async function foo (): Promise<string> {
  await bar();
  return Promise.resolve(null); // no inner type check here, but you'd usually await anyway
}

This does get a check:

function foo (): Promise<string> {
  return Promise.resolve(null); // throws
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I check if a JavaScript function returns a Promise?
Call the function, use instanceof let possiblePromise = f1(); let isPromise = possiblePromise instanceof Promise;.
Read more >
Javascript: How to access the return value of a Promise object
It's really important to note that the Promise object doesn't return a value, it resolves a value via the then() method. It is...
Read more >
Check if a Value is a Promise using JavaScript | bobbyhadz
To check if a value is promise, check if the type of the value is an object and has a property named then...
Read more >
Promise.resolve() - JavaScript - MDN Web Docs
The Promise.resolve() method "resolves" a given value to a Promise . If the value is a promise, that promise is returned; if the...
Read more >
Detect if Value Is a Promise in Node.js and JavaScript
You can use this behavior to determine whether a given value is a promise. Because promises implement the .then() method, you can check...
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