Asynchronous Calls and Recursion with Node.js

后端 未结 6 720
[愿得一人]
[愿得一人] 2021-02-09 10:34

I\'m looking to execute a callback upon the full completion of a recursive function that can go on for an undetermined amount of time. I\'m struggling with async issues and was

6条回答
  •  Happy的楠姐
    2021-02-09 11:29

    I think you might find caolan/async useful. Look especially into async.waterfall. It will allow you to pass results from a callback from another and when done, do something with the results.

    Example:

    async.waterfall([
        function(cb) {
            request.get({
                url: 'aaa.com'
            }, function(err, res, body) {
                if(err) {
                    return cb(err);
                }
    
                cb(null, JSON.parse(body).id);
            });
        },
        function(id, cb) {
            // do that otherFunc now
            // ...
            cb(); // remember to pass result here
        }
    ], function (err, result) {
       // do something with possible error and result now
    });
    

提交回复
热议问题