Node.js:如何使用异步模块执行无限循环

尤西

我需要进行一次HTTP调用,然后将响应放入数据库中。我应该永远重复一遍。我一直在阅读异步模块,但我不知道如何将这些动作与每次迭代之间的等待时间结合起来。

有人可以帮忙吗?

提前致谢。

格雷格

看看async.forever您的代码如下所示:

var async = require("async");
var http = require("http");

//Delay of 5 seconds
var delay = 5000;

async.forever(

    function(next) {

        http.get({
            host: "google.com",
            path: "/"
        }, function(response) {

            // Continuously update stream with data
            var body = "";

            response.on("data", function(chunk) {
                body += chunk;
            });

            response.on("end", function() {

                //Store data in database
                console.log(body);

                //Repeat after the delay
                setTimeout(function() {
                    next();
                }, delay)
            });
        });
    },
    function(err) {
        console.error(err);
    }
);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章