Skip to content

Instantly share code, notes, and snippets.

@nonsensery
Created August 3, 2015 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nonsensery/847be84fcae9b57e6af3 to your computer and use it in GitHub Desktop.
Save nonsensery/847be84fcae9b57e6af3 to your computer and use it in GitHub Desktop.
Flawed async ArticleView.updateArticle()
// Can you spot the asynchonous operation in this code?
ArticleView.prototype.updateArticle = function (props) {
this.setState({ error: null, title: null, body: null });
try {
var article = await ArticleStore.fetch(props.articleID);
this.setState({ title: article.title, body: article.body });
} catch (err) {
this.setState({ error: 'Oh Noes!' });
});
};
@Semigradsky
Copy link

Correct code:

// Can you spot the asynchonous operation in this code?

ArticleView.prototype.updateArticle = async function (props) {
  this.setState({ error: null, title: null, body: null });

  try {
    var article = await ArticleStore.fetch(props.articleID);
    this.setState({ title: article.title, body: article.body });
  } catch (err) {
    this.setState({ error: 'Oh Noes!' });
  };
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment