0
如何重构下面的代码?Javascript,Ember 2,如何重构承诺的代码(也许还有异步/等待)
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));
})
);
})
)
);
});
此外,我想了解如何避免许多功能时,我有以下的皮棉规则对这个代码:
[eslint] Use named functions defined on objects to handle promises (ember/named-functions-in-promises)
如何使用异步/ AWAIT?
您如何使用此功能的响应?你甚至需要它吗?什么是'this.ok()'和'this.error()'? – Lux
这样的两个小函数:'console.log(error)'。无论如何,问题是这个代码。你会怎么做? –
'get()'有点混乱。在第一次和第二次使用时,它似乎是*异步*,而在第三次使用时它看起来是*同步*。这是什么? –