If just one promise in my array is resolved, I still anticipate rejection when using Promise.all

I am dealing with an array of promises that include simple API URLs to fetch Github users. I have tried deliberately putting a wrong URL in one of the values in the array, but it always resolves instead of rejecting. Any idea why this might be happening?

For reference, here is a link to my test case: https://jsbin.com/lapibalate/edit?js,console

let urlMap = [
  {
    url: 'https://api.github.com/users/andreepratama27',
  },
  {
    url: 'https://apiiiiiii.github.com/users/rizalfakhri12'
  }
]

let promises = urlMap.map(v => fetch(v))

Promise.all(promises)
  .then(res => console.log('RESOLVED'))
  .catch(err => console.log("REJECTED"))

Answer №1

The reason for this issue is that you are fetching data not from the specified URLs, but from [object Object] on your current URL (since the v property contains an object, not a string). To fix this, update the map to use the url property instead:

let promises = urlMap.map(v => fetch(v.url))

Here's the updated example:

let urlMap = [
      {
        url: 'https://api.github.com/users/andreepratama27',
      },
      {
        url: 'https://apiiiiiii.github.com/users/rizalfakhri12'
      }
    ]

    let promises = urlMap.map(v => fetch(v.url))

    Promise.all(promises)
      .then(res => console.log('RESOLVED'))
      .catch(err => console.log("REJECTED"))

Answer №2

According to the information provided in the resources:

The fetch concept differs from the jQuery.ajax() method in two primary ways:

The Promise returned by fetch() will not reject for HTTP error statuses such as 404 or 500. Instead, it will resolve normally (with ok status set to false), and will only reject in case of network failure or any hindrance that prevented the completion of the request.

Hence, this serves as the explanation.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Can a search engine algorithm be developed to display an iframe based on the keywords entered in the search query?

Is it possible to create a search engine algorithm in HTML, CSS, and JavaScript that takes keywords from a search bar input and displays the best solution (or solutions) through iframes? html, css, javascript Below is the code for the iframes and search ...

Problem encountered when iFrame Element does not resize properly within its container

I have a small VA project that involves extracting stats from another website. I've discovered that the only way to accomplish this is by using an iFrame with the clip function. Here is the website I am pulling data from: NWGlobalVA.com However, I&a ...

Use jquery or javascript to align a button to the center

Can the vote button be centered in the script below? <script type="text/javascript" charset="utf-8" src="http://static.polldaddy.com/p/6352993.js"></script> <noscript><a href="http://polldaddy.com/poll/6352993/">This is a le ...

Incorporate a dynamic fading effect for text and images using JQuery

I successfully implemented a Crossfade effect using Jquery: function doAnimationLoop(o, n, t, i, a) { fadeInOut(o, n, t, i, function() { setTimeout(function() { doAnimationLoop(o, n, t, i, a) }, a) ...

Tips for extracting data from various select drop-down menus within a single webpage

As a newcomer to JQuery, I apologize if this question seems basic. I have a page with 20 categories, each offering a selection of products in a drop-down menu. The user will choose a product from each category, triggering an ajax call to retrieve the pric ...

Tips on concealing a bootstrap card upon being placed in a designated landing area

I am currently working on a vanilla JavaScript drag and drop feature. The goal is to drag a bootstrap card and drop it into a landing zone. Once dropped, I want the card to be hidden until the user hovers over the drop area where it was placed. How can I ...

Display or conceal a text field in Vue 2/Vuetify 1.5 depending on whether a checkbox is selected, with the possibility of duplicated

I've created a product checkbox selection feature that dynamically shows or hides text fields based on the user's selection. Unfortunately, there is a glitch in the system where selecting the second item causes duplication of text fields from th ...

Swapping out the document.createElement() method for React.createElement()

In a JavaScript library, I utilize document.createElement() to create an HTMLElement. Now, I am looking to develop a React library with the same functionality. My initial thought was to substitute document.createElement() with React.createElement(). There ...

Issue with the transmission of FormData and string data to PHP

Struggling to properly handle sending file data and string data together through ajax to PHP. Having trouble retrieving the correct string data from $_POST[] in PHP, resulting in only receiving the initial alphabet of a string instead of the specific produ ...

What is the process for altering a route in React 18?

Previously, I relied on the withRouter Higher Order Component (HOC) along with props.history.push() function to manage routes. However, with the introduction of React 18, these options are no longer available. My current task involves editing a post and ...

Exploring the process of searching for an id within an array of objects received through axios in Vue 2

I am currently working on verifying whether the user is included in the list that I retrieve using axios. However, I am facing an issue where the FILTER option I have used consistently returns undefined or [], even when the user does exist in the array. A ...

The Bootstrap modals seem to be invisible within the Rails application

Currently, I am integrating jquery into the devise views of a sample rails application. My intention is to test it on a sample app before implementing it in a production code. The controller and view for welcome are already set up. The routes.rb file loo ...

How can I initiate an AJAX POST request over HTTPS?

Despite my efforts, I am consistently encountering a 404 error when attempting to make an Ajax POST request. The specific error message reads: "GET 404 (Not Found)." Could this issue be related to the site's use of https? Below is the code snippet t ...

What is the best method to dynamically navigate through JSON data and save an object at a specific level?

Here is an example json structure for a menu: { "menu": [{ "name": "vegetation", "id": "1", "children": [ { "name": "landuse", "id": "1.1", "children": [ ...

Blending MapLibre GL JS with ThreeLoader3dTiles

After much trial and error, I have successfully implemented a code snippet that displays a Cesium 3D tiles model. This code effectively sets up a basic scene, loads the Cesium model, and integrates it into the scene. import { useEffect, useRef, useState } ...

HTML is loaded repeatedly using the ajax load() function, without involving JSON

My idea involves the seamless loading of HTML files from one to another. The visual representation below can provide a better understanding. To illustrate, there are several HTML files - no1.html, no2.html, no3.html, no4.html, etc. - all sharing a common J ...

Checking if arrays are empty using ES6 conditional if statements

My JSX ES6 React if statement doesn't seem to be working. What could be the issue? const otherVariables = somethingElse; return ( ... <div> {if (props.student.length === null && props.teacher.length === null) => ( <p&g ...

Navigating AngularJS with multiple external files and folders

Recently dove into Angular and hit a roadblock with routing. I followed the setup instructions, but for some reason it's not functioning as expected. index.html: <!DOCTYPE html> <html lang="en> <head> <meta charset="utf-8> ...

Utilize React HOC (Higher Order Component) and Redux to retrieve data and pass it as props

In my quest to develop a Higher Order Component (HOC) that can execute methods to fetch data from the backend and display a loader mask during loading, I encountered a challenge. I aim to have the flexibility of passing different actions for various compon ...

Implementing a Search Box feature in React-leaflet version 3.1.0

Struggling to incorporate a searchbox feature into my react app. Encountering the error message "Attempted import error: 'MapControl' is not exported from 'react-leaflet'" with the latest version of react-leaflet. import { MapContainer, ...