Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "then" to strings which may be used elsewhere #159

Merged
merged 1 commit into from May 24, 2018

Conversation

@Avaq
Copy link
Contributor

commented Nov 10, 2017

The massive adoption of Promises (and introduction of async/await based on them) in JavaScript made many programs potentially vulnerable to "accidental Promises".

In short, a program might take user input and produce an object as such:

{
  [userInput]: AnyFunction
}

...when the object above is given to a Promise (by returning it from then, calling Promise.resolve, passing it into res or rej when constructing a new Promise, or awaiting/returning it from an async function), everything will seem fine unless the input is exactly "then". Once it's then, a Promise will assume that the object is another Promise, and in trying to assimilate this accidental Promise the function will be called. After that, one of three things will happen depending on what the AnyFunction does with its input:

  1. The function calls one of the continuations provided by the Promise, and the program continues with some unexpected data (this is highly unlikely)
  2. The program hangs and never terminates (if the function stores input in memory)
  3. The program terminates early, failing to execute any other chained Promises (the more likely case)

For more in-depth information, please refer to the appropriate sections in the articles I've written regarding this issue:

  1. Broken Promises - Specialized API
  2. A clarification with examples to the article above (see the part under "I never found myself creating an object with a then method")
Add "then" to strings which may be used elsewhere
The massive adoption of Promises made many programs potentially vulnerable to "accidental Promises".

In short, a program might take user input and produce an object as such:

```js
{
  [userInput]: AnyFunction
}
```

...when the object above is given to a Promise, nothing breaks until the user input is exactly `"then"`. Once it's *then*, a Promise will assume that the object as another Promise, and in trying to assimilate this accidental Promise the function will be called. After that, one of three things will happen

1. The function calls one of the continuations provided by the Promise, and the program continues with some unexpected data (this is highly unlikely)
1. The program hangs and never terminates (if the function stores input in memory)
1. The program terminates early, failing to execute any other chained Promises (the more likely case)

For more in-depth information, please refer to the appropriate sections in the articles I've written regarding this issue:

1. [Broken Promises - Specialized API](https://medium.com/@avaq/broken-promises-2ae92780f33#6828)
1. [A clarification with examples to the article above](https://medium.com/@avaq/im-referring-to-the-fact-that-a-promise-is-eagerly-evaluated-as-opposed-to-lazily-evaluated-5385cc519e3b#33cd) (see the part under "I never found myself creating an object with a then method")

@minimaxir minimaxir merged commit 8661d3f into minimaxir:master May 24, 2018

@Avaq Avaq deleted the Avaq:patch-1 branch Jul 22, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.