node.js中的链异步函数

node.js中的链异步函数,node.js,es6-promise,Node.js,Es6 Promise,我有一个异步函数,它将my_url作为参数,并在每次运行时更新其值: function getUser(my_url) { var options = {url: my_url}; request(options, function (body){ my_url = body.url; }); } 我想无限次地调用这个函数。例如:getUsera返回b,getUserb返回c等等。这里的最佳实践是什么?我试着让getUser以我的url作为值返回一个承诺,但我不知道如何重复调用它

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

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

我想无限次地调用这个函数。例如:getUsera返回b,getUserb返回c等等。这里的最佳实践是什么?我试着让getUser以我的url作为值返回一个承诺,但我不知道如何重复调用它

我不确定这是否是您想要的,但下面是一个使用递归和请求承诺的示例:

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);
      });
  }

如果您有这样的服务器,那么您可以很容易地解析出响应中的下一个URL。我加入了计数以防止无限递归。

您可以尝试类似的方法。我稍微更改了URL创建,以示例URL演示流程:

"严格使用",; const request=要求“请求”; 函数延迟{ 返回新Promiseresolve=>{setTimeoutresolve,ms;}; } 功能承诺书{ 返回新的PromiseSolve,拒绝=>{ 请求{url},err,res,body=>{ 如果错误,拒绝错误; 其他机构; }; }; } 异步函数主{ 试一试{ 让url为空https://example.com'; 而url!==null{ const body=等待承诺请求; url=`https://example.com?q=${ encodeURIComponentbody.slice0,Math.floorMath.random*50 }`; console.logurl; 等待1000; } }犯错误{ console.error; } };
听起来你想使用递归。听起来也是个坏主意。
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