最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js — Sleep required - Stack Overflow

programmeradmin2浏览0评论

Consider the following scenario:

Inside one of my cron jobs, I am requesting somebody else's service that allows request only 3600 seconds. The API is analogous to GetPersonForName=string. Consider that I have a few people in my database and I need to update their information whenever I possibly I can, I scan my database for all the people and call this API. Example

// mongodb-in-use
People.find({}, function(error, people){
    people.forEach(function(person){
        var uri = "=" + person.name
        request({
            uri : uri
        }, function(error, response, body){
            // do some processing here
            sleep(3600) // need to sleep after every request
        })
    })
})

Not sure if sleep is even an idea approach here, but I need to wait for 3600 seconds after every request I make.

Consider the following scenario:

Inside one of my cron jobs, I am requesting somebody else's service that allows request only 3600 seconds. The API is analogous to GetPersonForName=string. Consider that I have a few people in my database and I need to update their information whenever I possibly I can, I scan my database for all the people and call this API. Example

// mongodb-in-use
People.find({}, function(error, people){
    people.forEach(function(person){
        var uri = "http://example./GetPersonForName=" + person.name
        request({
            uri : uri
        }, function(error, response, body){
            // do some processing here
            sleep(3600) // need to sleep after every request
        })
    })
})

Not sure if sleep is even an idea approach here, but I need to wait for 3600 seconds after every request I make.

Share Improve this question asked Oct 30, 2013 at 20:08 p0lArisp0lAris 4,8208 gold badges48 silver badges80 bronze badges 3
  • 1 Rather than sleep, why not use setTimeout- also you may want to look into asyncjs (github./caolan/async) - it's awesome – NG. Commented Oct 30, 2013 at 20:09
  • How would I use setTimeout in side this loop for every person? Example please? – p0lAris Commented Oct 30, 2013 at 20:11
  • sha512boo posted an Answer saying "You can use { sleep } in this module https://www.npmjs./package/gytimer" – Scratte Commented Mar 21, 2021 at 21:48
Add a ment  | 

1 Answer 1

Reset to default 7

You can use setTimeout and a recursive function to acplish this:

People.find({}, function(error, people){
    var getData = function(index) {
        var person = people[index]

        var uri = "http://example./GetPersonForName=" + person.name
        request({
            uri : uri
        }, function(error, response, body){
            // do some processing here

            if (index + 1 < people.length) {
                setTimeout(function() {
                    getData(index + 1)
                }, 3600)
            }
        })
    }

    getData(0)
})
发布评论

评论列表(0)

  1. 暂无评论