K
K
komarevtsev2016-11-28 13:14:57
JavaScript
komarevtsev, 2016-11-28 13:14:57

How to terminate async waterfall?

Good afternoon. I am using the async module in node.js.
The question arose how to properly complete the execution of the async waterfall.
The logic is as follows: request a cache, if there is a cache - give an answer immediately, if there is no cache - make a request to api
So if there is a cache, you want to complete the waterfall right away. It is possible to pass the cache object as the first parameter in the callback as an error, but this is somehow ugly and may not be obvious to others. Are there any other options?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Eugene, 2016-11-28
@AppFA

Instead of it is easier to make on promises?
Pseudocode:

cache.get(key).then((data) => {
    if (data) return data;

    return api.something;
});

V
Vladimir Sergeevich, 2016-11-28
@VladimirZhid

Is a waterfall really needed here? Getting a cache is a blocking operation? If so, then you can wrap it in a regular promise. According to your description, it seems to me that it is possible to do without a
waterfall as it is needed to sequentially run functions with blocking operations and the ability to pass arguments from one to the next ...

T
tex0, 2016-11-28
@tex0

async waterfall, if I'm not mistaken, does not know how to dynamic flow. And you just have an algorithm with a condition. Use promises.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question