JavaScript Promise version of a “while” loop?

I've come to the realization that since promises in ECMAScript 6 allow for synchronous coding of asynchronous functions, for every promise-laden piece of code there's a synchronous corollary. For instance:

var data = processData(JSON.parse(readFile(getFileName())));

Is the same as:

var data = getFileName()
    .then(readFile)
    .then(JSON.parse)
    .then(processData);

Now for my current use-case I want to write code to pull data from a massive public API. The API is paginated, so in a purely synchronous world I would write something like the following:

var data = [];
var offset = 0;
var total = 10000; // For example - not actually how this would work
while( offset < total ) {
    data.concat(getDataFromAPI(offset));
    offset = data.length;
}

Now my question is, how would I do this with promises? I could write something like:

var data = [];
var offset = 0;
var total = 10000;
getDataFromAPI(offset)
    .then(function(newData){
        data.concat(newData);
        return getDataFromAPI(data.length);
    });

But at this point I'm forced to just chain infinite .thens -- there's no looping logic. I feel like something should be possible using recursion, but I have no idea how to do it.

I'm using BluebirdJS as my promise library, so I have access to all of their helper methods.

Answer:1

i'm using this JS code https://kolber.github.io/audiojs/docs/ and I was wondering if i can load and skipt to a specific point of the audio track. of if there is any other way to do it using an ...

i'm using this JS code https://kolber.github.io/audiojs/docs/ and I was wondering if i can load and skipt to a specific point of the audio track. of if there is any other way to do it using an ...

I have written code to return a new array containing only the unique elements of two seperate arrays. example: [1,2,3] and [1,2] will return 3. I have working code, however I would just like an ...

I have written code to return a new array containing only the unique elements of two seperate arrays. example: [1,2,3] and [1,2] will return 3. I have working code, however I would just like an ...

  1. javascript return array from function
  2. javascript return array of objects
  3. javascript return array index
  4. javascript return array without last element
  5. javascript return array value
  6. javascript return array as string
  7. javascript return array length
  8. javascript return array from foreach
  9. javascript return array undefined
  10. javascript return array without first element
  11. javascript return array key value
  12. javascript return array of object values
  13. javascript return array after push
  14. javascript return array of properties
  15. javascript return array range
  16. javascript return array without duplicates
  17. javascript return array from promise
  18. javascript return array of arrays
  19. javascript return array without element
  20. javascript return array with item removed

function chart(){ Barchart(); Linechart(); function Barchart(){ } function Linechart(){ } } Chart() is main parent function. Inside I have two chart ...

function chart(){ Barchart(); Linechart(); function Barchart(){ } function Linechart(){ } } Chart() is main parent function. Inside I have two chart ...

  1. call function inside function python
  2. call function inside function
  3. call function inside class python
  4. call function inside function javascript
  5. call function inside for loop javascript
  6. call function inside another function python
  7. call function inside class javascript
  8. call function inside useeffect
  9. call function inside react component
  10. call function inside iframe
  11. call function inside object javascript
  12. call function inside function c++
  13. call function inside class c++
  14. call function inside string javascript
  15. call function inside function powershell
  16. call function inside html
  17. call function inside module.exports
  18. call function inside lambda python
  19. call function inside bash script
  20. call function inside promise

For a web project, I've included a JavaScript file as a script src, as shown here. <script src="xml2json.js"> //same directory as the web project Next, I tried to invoke a method within ...

For a web project, I've included a JavaScript file as a script src, as shown here. <script src="xml2json.js"> //same directory as the web project Next, I tried to invoke a method within ...

  1. external javascript file example
  2. external javascript file html
  3. external javascript file not working
  4. external javascript file not loading
  5. external javascript file in html example
  6. external javascript file can contain
  7. external javascript file w3schools