Skip to main content
added 858 characters in body
Source Link
Terry Lennox
  • 30.9k
  • 5
  • 36
  • 45

There are a few ways you can tackle this. One way is to use the async library, this allows to to run async. calls in parallel, or in series.

For example, we can use async.mapSeries() to get the result of a series of asynchronous calls as an array.

You input your array of ids, then the callback will return an array of the data returned, for example, I've mocked out the getChapter() function to give you an idea of how it would work:

// Mock out epub object
const epub = { 
    getChapter(id, callback) {
        setTimeout(() => callback(null, "Data for id " + id), 250);
    }
}

let ids = [1,2,3,4];
console.log("Calling async.mapSeries for ids:", ids);
async.mapSeries(ids, epub.getChapter, (err, result) => {
    if (err) {
        console.error(err);
    } else {
        console.log("Result:", result)
    }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/async/3.2.0/async.min.js" integrity="sha512-6K6+H87tLdCWvY5ml9ZQXLRlPlDEt8uXmtELhuJRgFyEDv6JvndWHg3jadJuBVGPEhhA2AAt+ROMC2V7EvTIWw==" crossorigin="anonymous"></script>

You could also promisify the epub call and use Promise.all to get the result, like so:

epub = { 
    getChapter(id, callback) {
        setTimeout(() => callback(null, "Data for id " + id), 250);
    }
}
 
let ids = [1,2,3,4];

function getChapterPromisified(id) {
    return new Promise((resolve, reject) => { 
        epub.getChapter(id, (err, data) => { 
            if (err) {
                reject(err);
            } else {
                resolve(data);
            }
        })
    })
}

// Create a list of promises, one for each call
const promises = ids.map(id => getChapterPromisified(id))
Promise.all(promises)
    .then(result => console.log("Result:", result))
    .catch(error => console.error("An error occurred:", err));

There are a few ways you can tackle this. One way is to use the async library, this allows to to run async. calls in parallel, or in series.

For example, we can use async.mapSeries() to get the result of a series of asynchronous calls as an array.

You input your array of ids, then the callback will return an array of the data returned, for example, I've mocked out the getChapter() function to give you an idea of how it would work:

const epub = { 
    getChapter(id, callback) {
        setTimeout(() => callback(null, "Data for id " + id), 250);
    }
}

let ids = [1,2,3,4];
console.log("Calling async.mapSeries for ids:", ids);
async.mapSeries(ids, epub.getChapter, (err, result) => {
    if (err) {
        console.error(err);
    } else {
        console.log("Result:", result)
    }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/async/3.2.0/async.min.js" integrity="sha512-6K6+H87tLdCWvY5ml9ZQXLRlPlDEt8uXmtELhuJRgFyEDv6JvndWHg3jadJuBVGPEhhA2AAt+ROMC2V7EvTIWw==" crossorigin="anonymous"></script>

There are a few ways you can tackle this. One way is to use the async library, this allows to to run async. calls in parallel, or in series.

For example, we can use async.mapSeries() to get the result of a series of asynchronous calls as an array.

You input your array of ids, then the callback will return an array of the data returned, for example, I've mocked out the getChapter() function to give you an idea of how it would work:

// Mock out epub object
const epub = { 
    getChapter(id, callback) {
        setTimeout(() => callback(null, "Data for id " + id), 250);
    }
}

let ids = [1,2,3,4];
console.log("Calling async.mapSeries for ids:", ids);
async.mapSeries(ids, epub.getChapter, (err, result) => {
    if (err) {
        console.error(err);
    } else {
        console.log("Result:", result)
    }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/async/3.2.0/async.min.js" integrity="sha512-6K6+H87tLdCWvY5ml9ZQXLRlPlDEt8uXmtELhuJRgFyEDv6JvndWHg3jadJuBVGPEhhA2AAt+ROMC2V7EvTIWw==" crossorigin="anonymous"></script>

You could also promisify the epub call and use Promise.all to get the result, like so:

epub = { 
    getChapter(id, callback) {
        setTimeout(() => callback(null, "Data for id " + id), 250);
    }
}
 
let ids = [1,2,3,4];

function getChapterPromisified(id) {
    return new Promise((resolve, reject) => { 
        epub.getChapter(id, (err, data) => { 
            if (err) {
                reject(err);
            } else {
                resolve(data);
            }
        })
    })
}

// Create a list of promises, one for each call
const promises = ids.map(id => getChapterPromisified(id))
Promise.all(promises)
    .then(result => console.log("Result:", result))
    .catch(error => console.error("An error occurred:", err));

Source Link
Terry Lennox
  • 30.9k
  • 5
  • 36
  • 45

There are a few ways you can tackle this. One way is to use the async library, this allows to to run async. calls in parallel, or in series.

For example, we can use async.mapSeries() to get the result of a series of asynchronous calls as an array.

You input your array of ids, then the callback will return an array of the data returned, for example, I've mocked out the getChapter() function to give you an idea of how it would work:

const epub = { 
    getChapter(id, callback) {
        setTimeout(() => callback(null, "Data for id " + id), 250);
    }
}

let ids = [1,2,3,4];
console.log("Calling async.mapSeries for ids:", ids);
async.mapSeries(ids, epub.getChapter, (err, result) => {
    if (err) {
        console.error(err);
    } else {
        console.log("Result:", result)
    }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/async/3.2.0/async.min.js" integrity="sha512-6K6+H87tLdCWvY5ml9ZQXLRlPlDEt8uXmtELhuJRgFyEDv6JvndWHg3jadJuBVGPEhhA2AAt+ROMC2V7EvTIWw==" crossorigin="anonymous"></script>