PromiseProducer must not be async
sjungwirth opened this issue ยท 8 comments
Ran into an issue where I wanted to have an async PromiseProducer work function, but since an async function always returns a promise, the pool would never end.
Not sure if this is truly an issue but a note in the documentation might help others from the same pitfall.
The producer itself can't be async, because indeed, the async
keyword is syntactic sugar for returning a Promise from the function. It's still possible to use async
though:
const doSomethingAsync = async () => {
await somethingElse()
}
const producer = () => {
if (someCondition) {
return doSomethingAsync()
} else {
return null
}
}
Are there any plans to support async producers?
My use case is consuming a database cursor. cursor.next() is async, and then I want to do an async operation with the item, or return null from the producer if the item is null.
That'd get pretty compilcated, I think. We'd need to support a return type of Promise<Promise<?>>
for the producer.
I suppose it could be handled externally by flattening the higher-order promise in the producer, and maintain some internal state to eventually return null
upon the next call. Doesn't sound pleasant either though, I'll admit.
I think I'd rather see an explicit API call to stop/end the pool: pool.end()
.
Sorry for bumping an old thread, but implementing an async generator API could solve the problem (available in node v10), you can even do that in a loop (potentially an endless one so the pool never stops)
async function* producer() {
await do_something()
yield the_promise
}
I could be wrong, but if your platform supports something as recent as async generators, doesn't the value of es6-promise-pool decrease to the point where you might as well implement the logic yourself in a few lines of modern JS? Even though I originally authored this package, I must admit that the rapid evolution of the language has mostly made it obsolete for me.
Hi @timdp - I know this discussion is a few years old but I found es6-promise-pool to be a solution to a problem I'm trying to solve. Yet you mentioned that even you think it's obsolete. Would you be able to point me and other future readers at a more modern solution assuming the latest JS features are available? Much appreciated
See the readme. ๐