node.js中的链异步函数

时间:2019-02-28 20:46:31

标签: node.js es6-promise

我有一个异步函数,该函数以my_url作为参数,并在每次运行时更新其值:

function getUser(my_url) {
 var options = {url: my_url};
 request(options, function (body){
    my_url = body.url;
 });
}

我想无限次调用此函数。例如:getUser(“ a”)返回“ b”,getUser(“ b”)返回“ c”,依此类推。这里的最佳做法是什么?我试图使getUser()返回一个promise作为值的my_url,但是我不知道如何重复调用它。

3 个答案:

答案 0 :(得分:1)

您可以尝试这样的操作(我对URL创建进行了一些更改,以使用示例URL演示流程):

let i = -1;
let time = 5000;
let slideTimer;
let slides = document.getElementsByClassName('slide');
let slideDots = document.getElementsByClassName('dot');    
document.addEventListener("DOMContentLoaded", function(event) {
window.onload = changeSlide();
});

function clickChangeSlide(n){
    clearTimeout(slideTimer);
    changeSlide(n, true);
  }

  function changeSlide(n = i, manual = false){

    for(let j = 0; j < slides.length; j++){
      if(j == i) {
        slides[j].classList.add('prev-slide');
        slides[j].classList.remove('active-slide');
        slideDots[j].classList.remove('active-dot');
        continue; 
      }
      slideDots[j].classList.remove('active-dot');
      slides[j].classList.remove('prev-slide'); 
      slides[j].classList.remove('active-slide');
    }
    if(manual){
      if(n < 0) i = slides.length - 1
      else if(n > slides.length - 1) i = 0
      else i = n
    }else i = i < slides.length - 1 ? i+1 : 0;

    slides[i].classList.add('active-slide');
    slideDots[i].classList.add('active-dot');

    slideTimer = setTimeout(changeSlide, time);
  }

答案 1 :(得分:-1)

function getUser(my_url) {
      var options = {url: my_url};

      if(my_url == "some stop condition") return ;

      request(options, function (body){
         my_url = body.url;
      }).then( response => {
           getUser(response);
      });
  }

答案 2 :(得分:-1)

我不确定这是否是您要查找的内容,但这是使用递归和request-promise的示例:

const rp = require('request-promise')

const url = "https://google.com"
var options = {
    uri: url
}

var count = 0

function chain(options, count) {
    const newURL = `${url}/?count=${count}`
    console.log(`Requesting ${newURL}`)
    rp(options).then(function(body) {
        console.log(`Success, body was ${body.length} bytes`)
        count = count + 1;
        if ( count < 20 ) {
            options = {
                uri: newURL
            }
            // recursion

            chain(options, count)
        }
    }).catch(function (err) {
        console.log(`An error occurred: ${err}`)
    })
}

chain(options, count)

运行此命令时,输出如下:

Requesting https://google.com/?count=0
Success, body was 45855 bytes
Requesting https://google.com/?count=1
Success, body was 45861 bytes
Requesting https://google.com/?count=2
Success, body was 45864 bytes
Requesting https://google.com/?count=3
Success, body was 45875 bytes
Requesting https://google.com/?count=4
Success, body was 45859 bytes
Requesting https://google.com/?count=5
Success, body was 45851 bytes
Requesting https://google.com/?count=6
Success, body was 45882 bytes
Requesting https://google.com/?count=7
Success, body was 45843 bytes
Requesting https://google.com/?count=8
Success, body was 45892 bytes
Requesting https://google.com/?count=9
Requesting https://google.com/?count=9
Success, body was 45835 bytes
Requesting https://google.com/?count=10
Success, body was 45885 bytes
Requesting https://google.com/?count=11
Success, body was 45865 bytes
Requesting https://google.com/?count=12
Success, body was 45851 bytes
Requesting https://google.com/?count=13
Success, body was 45859 bytes
Requesting https://google.com/?count=14
Success, body was 45905 bytes
Requesting https://google.com/?count=15
Success, body was 45848 bytes
Requesting https://google.com/?count=16
Success, body was 45896 bytes
Requesting https://google.com/?count=17
Success, body was 45879 bytes
Requesting https://google.com/?count=18
Success, body was 45877 bytes
Requesting https://google.com/?count=19
Success, body was 45844 bytes

如果您的服务器具有这种行为,则可以轻松解析出响应then()中的下一个URL。我加入了计数以防止无限递归。

相关问题