{"size": 1502, "ext": "js", "max_stars_repo_path": "7 Asynchronous JS/2 chainingPromises.js", "max_stars_repo_name": "mgomez08/JS-Concepts", "max_stars_repo_stars_event_min_datetime": "2020-09-10T17:12:46.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-07T05:48:42.000Z", "max_issues_repo_path": "7 Asynchronous JS/2 chainingPromises.js", "max_issues_repo_name": "mgomez08/JS-Concepts", "max_issues_repo_issues_event_min_datetime": "2020-09-29T13:08:11.000Z", "max_issues_repo_issues_event_max_datetime": "2020-10-10T17:04:43.000Z", "max_forks_repo_path": "7 Asynchronous JS/2 chainingPromises.js", "max_forks_repo_name": "mgomez08/JS-Concepts", "max_forks_repo_forks_event_min_datetime": "2020-09-18T16:26:21.000Z", "max_forks_repo_forks_event_max_datetime": "2020-10-10T14:42:43.000Z"}
{"max_stars_count": 13, "max_issues_count": 20, "max_forks_count": 18, "avg_line_length": 36.6341463415, "max_line_length": 128, "alphanum_fraction": 0.6664447403}
// ** Making multiple promises working together ** \\
const promise1 = new Promise((resolve, reject) => {
    setTimeout(resolve, 200, 'Hello')
})

const promise2 = new Promise((resolve, reject) => {
    setTimeout(resolve, 500, 'are')
})

const promise3 = new Promise((resolve, reject) => {
    setTimeout(resolve, 1000, 'you')
})

const promise4 = new Promise((resolve, reject) => {
    //setTimeout(reject, 4000, 'there?')
    reject('naaa')
})

Promise.all([promise1, promise2, promise3, promise4])
    .then(values => console.log('values show after the max timer ends', values)) // Will run after all the promises are resolved
    .catch((err) => console.log(err)) // If even one is rejected, the operation will land in .catch()
    .finally(() => { console.log('this piece of code runs irrespective of resolve or reject') })
    // Finally runs after the promise chain is completed no matter if it is resolved or rejected
    
// ** chaining fetch requests ** \\
/**
 * Below code works only on console
 * Source for the API: https://jsonplaceholder.typicode.com
 */
const urls = [
    'https://jsonplaceholder.typicode.com/users',
    'https://jsonplaceholder.typicode.com/posts',
    'https://jsonplaceholder.typicode.com/photos'
]
Promise.all(urls.map(url => {
    return fetch(url).then((response) => { return response.json() })
})).then((results) => {
    results.forEach(result => {
        console.log(result); // Log the values returned by the API
    })
}).catch(err => console.log(err))