vendredi 8 septembre 2017

Javascript, Ember 2, How to refactor this code of promises (maybe also with async/await)

How can I refactor the code below?

get(category, "posts").then(posts => {
  return all(
    posts.map(post =>
      get(post, "words").then(words => {
        return all(
          words.map(word => {
            if (!get(word, "hasDirtyAttributes")) {
              return false;
            }
            return word
              .save()
              .then(() => this.ok())
              .catch(error => this.error(error));
          })
        );
      })
    )
  );
});

Also I would like to understand how to avoid having many functions when I have the following lint rule on this code:

[eslint] Use named functions defined on objects to handle promises (ember/named-functions-in-promises)

How can I use async/await?




Aucun commentaire:

Enregistrer un commentaire