I'v got the following code block:
new Promise((res, rej) => {
if (checkCondition()) {
res(getValue())
} else {
getValueAsync((result) => {
res(result)
})
}
}).then((value) => {
doStuff(value)
})
I want to use the async
library, which is what the rest of the codebase uses. I'm trying to figure out which function best solves this problem. I've only beena ble to come up with one solution, but it seems a bit clunky:
async.tryEach([
(cb) => {
if (checkCondition()) {
cb (null, getValue())
} else {
cb (1)
}
},
(cb) => {
if (!checkCondition()) {
getValueAsync((result) => {
cb(null, result)
})
} else {
cb (1)
}
}],
(err, value) => {
doStuff(value)
}
)
Like I said, this feels clunky. It's nice that it runs the checks asynchronously and I'm pretty sure it will always get the right result, but the code block is now much bigger and more confusing.
My only purpose here is to use the same libraries and techniques as the rest of the codebase, which doesn't use promises at all. I haven't been able to find a similar control flow block anywhere, which is why I needed to construct it myself. If I'm stuck with promises, so be it, but I'd like to find a solution using the library.