chain async functions in node.js

I have an async function, which takes a my_url as parameter and update its value each time it runs :

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

I want to call this function indefinite times. For example: getUser("a") returns "b", getUser("b") return "c" and so on. What's the best practice here? I've tried making getUser() returns a promise with my_url as value, but I don't know how to call it repeatedly.

You can try something like this (I've changed URL creation a bit to demonstrate the flow with an example URL):

'use strict';

const request = require('request');

function delay(ms) {
  return new Promise((resolve) => { setTimeout(resolve, ms); });
}

function promisifiedRequest(url) {
  return new Promise((resolve, reject) => {
    request({ url }, (err, res, body) => {
      if (err) reject(err);
      else resolve(body);
    });
  });
}

(async function main() {
  try {
    let url = 'https://example.com';

    while (url !== null) {
      const body = await promisifiedRequest(url);
      url = `https://example.com?q=${
        encodeURIComponent(body.slice(0, Math.floor(Math.random() * 50)))
      }`;
      console.log(url);
      await delay(1000);
    }
  } catch (err) {
    console.error(err);
  }
})();

chaining async method calls - javascript, Foo.prototype.bar = function () { return new Promise(function (resolve) messel @messels-MBP:~/Desktop/Dropbox/code/js/async-chain$ node index.js bar� An async function is a function declared with the async keyword. Async functions are instances of the AsyncFunction constructor, and the await keyword is permitted within them. The async and await keywords enable asynchronous, promise-based behavior to be written in a cleaner style, avoiding the need to explicitly configure promise chains.

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

Mastering Async Await in Node.js, works only inside async functions let value = await promise ; example from the chapter Promises chaining and rewrite it using async/await : I'm using node.js a lot and using promises a lot, using observables a lot. I don't� It has 2 modes you can use: parallel or serial. In parallel mode, it runs the async functions in parallel (this is a lie as nothing in parallel in node.js, but it will simply context switch based on I/O calls as any async stuff works in node). In serial mode, it runs each function after the previous one finishes by chaining the callbacks together.

I'm not sure if this is what you're looking for, but here's an example using recursion and 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)

When I run this here's the output:

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

You could easily parse out the next URL in the response then() if you had a server that behaved that way. I put in the count to prevent infinite recursion.

Promises chaining, As of version 7.6, Node.js also ships with async/await enabled by default. This is how you would chain asynchronous operations using� Async functions are available natively in Node and are denoted by the asynckeyword in their declaration. They always return a promise, even if you don’t explicitly write them to do so. Also, the awaitkeyword is only available inside async functions at the moment - it cannot be used in the global scope.

Async/await, finally method in this post. Chains of promises gives you the ability to use the values returned by asynchronous functions to select and control the� When the asyncfunction is called, it returns with a Promise. When the asyncfunction returns a value, the Promisegets fulfilled, if the asyncfunction throws an error, it gets rejected. The awaitkeyword can be used to wait for a Promiseto be resolved and returns the fulfilled value.

Simplifying Asynchronous Coding with Async Functions, In parallel mode, it runs the async functions in parallel (this is a lie as nothing in parallel in node.js, but it will simply context switch based on I/O calls as any� Async functions are a combination of promises and generators, and basically, they are a higher level abstraction over promises. Let me repeat: async/await is built on promises. Why were async/await introduced? They reduce the boilerplate around promises, and the "don't break the chain" limitation of chaining promises.

Advanced JavaScript Node.js Promises chaining collections, Async functions are a combination of promises and generators, and basically, promises, and the “don't break the chain” limitation of chaining promises. and check out my premium React/Vue/Svelte/Node/Next.js courses! In this post, we cover what tools and techniques you have at your disposal when handling Node.js asynchronous operations: async.js, promises, and async functions. After reading this article, you’ll know how to use the latest async tools at your disposal provided by Node.js!

Comments
  • Sounds like you want to use recursion. Also sounds like a bad idea.