0

I am trying to string multiple fetches while also performing a promise on each of them. My goal is after every fetch is 1. to parse the data retrieved and 2. append it to an object as demonstrated by each.scripture = data. After this procedure is performed on each url, I want to 3. resolve the now changed data that was originally passed into the function header. Can someone please assist me with this? As you can see I have attempted this, but this does not work as intended.

function getScripture(data) {
    var feed = []
    return new Promise((resolve, reject)=> {
        data.map((each)=> {
            fetch(`https://getbible.net/json?passage=${each.book}${each.chapter}:${each.verse}&version=web`, {
                'method': 'GET',
                'headers': {
                    'Content-Type': 'application/json'
                }
            })
            .then((response) => response.text())
            .then((data) => {
                data = JSON.parse(data.substring(1,data.length - 2))
                data = data['book'][0]['chapter'][each.verse.toString()]['verse']
                each.scripture = data
                feed.concat(each)
            })
            .catch((err)=> reject(err))
        })
        resolve(feed)
    })
} 
William Sheppard
  • 105
  • 2
  • 12
  • See the `Promise.all` example in [this answer](https://stackoverflow.com/a/43766002/283366) – Phil May 15 '20 at 01:35
  • Hey, I understand this stuff is probably very easy for you to grasp, but I am still struggling to figure out how to integrate my solution with the answer provided in your link. Clearer direction would be appreciated. @Phil – William Sheppard May 15 '20 at 02:03
  • `fetch` returns a `Promise`, just like `doSomethingAsync` in that answer – Phil May 15 '20 at 02:06
  • Something like this ~ https://pastebin.com/TjZg5FpA. That will return a promise that resolves with an array of modified `each`'s – Phil May 15 '20 at 02:15

0 Answers0