Closed SubJunk closed 4 years ago
What's wrong with thenable promises?
@vankasteelj nothing wrong with them, it's just a bit more readable IMO. One issue I have with them is the placement of return
statements throughout a function. In a long chain of then
s, especially when there are more nested then
s within it, it can get hard to follow the logic and starts to look similar to Callback Hell.
For example:
search(info) {
let subs = Array()
return this.login()
.then(() => libsearch.optimizeQueryTerms(info))
.then(optimizedQT => {
return Promise.all(optimizedQT.map(op => {
return this.api.SearchSubtitles(this.credentials.status.token, [op]).then(result => subs = subs.concat(result.data))
}))
})
.then(() => libsearch.optimizeSubs(subs, info))
.then(list => libsearch.filter(list, info))
}
The return
is up by the login step, but that's not what returns. It could be:
async search(info) {
let subs = Array()
await this.login()
const optimizedQT = await libsearch.optimizeQueryTerms(info)
await Promise.all(optimizedQT.map(op => {
return this.api.SearchSubtitles(this.credentials.status.token, [op]).then(result_1 => subs = subs.concat(result_1.data));
}))
const list = await libsearch.optimizeSubs(subs, info)
return await libsearch.filter(list, info)
}
which in my opinion is more easy to read and understand.
Okay I get the point.
But that isn't readable to me, as I don't get what each term does and it seems counterintuitive (to me, I've never learned await/async. I write ugly js on my spare time so I have the tools I need, I'm not a programmer per se^^), so if I'm to maintain this, I need to follow my own logic.
No problem, just a suggestion :)
Could make it more readable, I would also be happy to do it. What do you think?