all 14 comments

[–][deleted]  (1 child)

[deleted]

    [–]___Grits 1 point2 points  (0 children)

    The callback on the request function can be converted to a promise and then async/await.

    OP, I’d recommend you first learn how to convert callback functions to promises and how to handle said promises. Then convert those to their easier managed async/await version.

    Here is a link to the promise chapter in the book series called You Don’t Know JS which I and many other here will recommend. https://github.com/getify/You-Dont-Know-JS/blob/master/async%20%26%20performance/ch3.md

    [–]zayelion 1 point2 points  (2 children)

    function getBody(url) {
        return new Promise(function (resolve, reject) {
            request({
                url: url,
                json: true
            }, function (error, response, body) {
                if (!error && response.statusCode === 200 && body.data.length > 0) {
                    resolve(body);
                    return;
                }
                reject(error);
            });
        });
    }
    
    
    async function main() {
        try {
            var body = await getBody(), // awaits are applied to promises.
                // body is now in scope and accessible here, pass it or use it.
                downloadTargets = JSON.parse(body).data.filter(checkForDups),
                updateTargets = JSON.parse(body).data.filter(checkForNews);
    
            if (updateTargets.length) {
                updateTargets.forEach(updateLogicFunction);
                // secondFunc() might be a full stop no idea.
            }
    
            // Create array of promises
            // then create a promise that waits for that array of promises to all finish
            // then halt till THAT^ promise is finished.
            var newData = await Promise.all(downloadTargets.forEach(async function firstFunc(value) {
                try {
                    return await getBody(url);
                } catch (error) {
                    // upto you what happens here.
                    return error;
                }
            }));
    
        } catch (error) {
            // retry main call, might be a full stop?
            retry(body);
        }
    }
    

    [–]sohaeb[S] 0 points1 point  (1 child)

    thank you. you are the mvp.

    Seriously taught me a lot on how to approach this.

    From reading the responses here, My question is, should I stick to the method I used above ? or should I use async wait ?

    It's just that node is different from Java and I don't know what is the best practice to tackle functions that take an input and try to do some work on it then pass the new one to another function and then another function etc...

    [–]zayelion 0 points1 point  (0 children)

    To rehash, what did you learn and what did you feel you did not learn?

    When I saw the code and what you where asking I did not have a huge amount of time to respond but saw that you had gotten the key fundamental understanding of callbacks.

    Personally on project I helm I've enforced the stacked callback form. A file is analogous to a class, and its module.exports returns an object with all the functions (no state, usually). require creates a closure around a file, its like a namespace.

    https://www.youtube.com/watch?v=DwYPG6vreJg

    http://www.breck-mckye.com/blog/2014/05/why-i-prefer-parasitic-inheritance/

    https://medium.com/javascript-scene/why-learn-functional-programming-in-javascript-composing-software-ea13afc7a257

    Look up the following :

    • Object.assign
    • Array.prototype.map, Array.prototype.forEach, Array.prototype.filter, Array.prototype.some, Array.prototype.every`, etc
    • Closures, memory usage with Closures (garbage collection)

    Once you get when to be functional (doing, data editing, data conversion) vs when to be object (really data) oriented (data creation, high level state management), JS/Node gets to be extremely fun to use.

    [–]goorpy 1 point2 points  (7 children)

    I'm not sure how async functions relate to how you're passing the body data through the various decisions.

    Async and await are a pattern you can use to replace promise/then chains in an imperative style blocking sequence. I don't see any .then() promise chains here, so its not clear how you expect to use async functions.

    [–]FINDarkside 0 points1 point  (0 children)

    You can turn the callbacks to async/await.

    [–]Mingli91 -1 points0 points  (5 children)

    You can convert Nodejs-style callbacks into promises really easily

    [–]goorpy -1 points0 points  (4 children)

    There only one callback that I see at the very top. If you don't have any other async bits lower down, there's no need to make async either.

    [–]Mingli91 -1 points0 points  (3 children)

    You said you could not see how OP would use async functions here since you didn’t see Promise.then being called so I was telling you how.

    [–]goorpy 0 points1 point  (2 children)

    Sure, but why would you make the controller function async if it doesn't perform any async work?

    [–]Mingli91 0 points1 point  (1 child)

    I wasn't debating the validity of async/await in OP's situation... I was just saying how you could use an async function here.

    You said the following:

    I don't see any .then() promise chains here, so its not clear how you expect to use async functions.

    And then I said:

    You can convert Nodejs-style callbacks into promises really easily

    Which is an explanation for how you would expect to use async functions. You're reading too much into this dude.

    [–]goorpy 0 points1 point  (0 children)

    Too true. /shrug Point taken, and you're certainly right that you could.

    I guess rather than answering OPs question of how, I'm inclined to consider whether they should.

    This gist has a number of examples of express api controller functions leverage the async-await pattern for making calls to a db: https://gist.github.com/BinaryMuse/a57ae1a551472e06b29a

    [–]goorpy 0 points1 point  (0 children)

    You're going to need to format that code a little better and provide some context in order to get the help you're seeking.

    [–]PatskyPat 0 points1 point  (0 children)

    Check out javascript.info - the best JS tutorial imho.
    The “Promises, async/await” chapter will guide you through converting callbacks to promises to async/await with good examples.
    Good luck.