Diverse behaviors exhibited by an array of promises

I've developed a function that generates an array of promises:

    async addDefect(payload) {
    this.newDefect.setNote(payload.note);
    this.newDefect.setPriority(payload.priority);
    const name = await this.storage.get(StorageKeys.NAME);
    const movingFilesJob = this.cachedPhotoUrls.map(url => {
      const defectImage = this.newDefect.generateImageUrl(name);
      return this.file.moveImageToAppFile(url, defectImage.url);
    });
    await Promise.all(movingFilesJob);
    this.viewCtrl.dismiss(this.newDefect);
  }

Now, I want to move the creation of movingFilesFob to another class. So, I created the following function:

async persistPhotos(photoBuffer: string[], defect: Defect) {
    const name = await this.storage.get(StorageKeys.NAME);
    return photoBuffer.map(url => {
      const defectImage = defect.generateImageUrl(name);
      return this.file.moveImageToAppFile(url, defectImage.url);
    });
  }

When I try to replace the code, I'm encountering the error message:

Argument of type 'Promise[]>' is not assignable to parameter of type 'Iterable<{} | PromiseLike<{}>>'.   Property '[Symbol.iterator]' is missing in type 'Promise[]>'

In my function call, it looks like this:

async addDefect(payload) {
    this.newDefect.setNote(payload.note);
    this.newDefect.setPriority(payload.priority);
    const name = await this.storage.get(StorageKeys.NAME);
    const movingFilesJob = this.photo.persistPhotos(this.cachedPhotoUrls, this.newDefect);
    await Promise.all(movingFilesJob);
    this.viewCtrl.dismiss(this.newDefect);
  }

It's puzzling why the first example works fine while the second one doesn't. I tried assigning type :any to the return value but it didn't work during runtime.

Answer №1

Providing a direct response to the query at hand:

Why is it that the code functions correctly in the initial example but not in the subsequent one? I tried assigning type :any for the return, but it still doesn't work during runtime.

The issue lies in inadvertently altering the return type.

Initial Code Snippet:

const movingFilesJob = this.cachedPhotoUrls.map(...)

In this snippet, an array is being assigned to movingFilesJob.

Rectified Version:

return photoBuffer.map(...)

This modification generates an array of Promise objects from persistPhotos(), whereas the async keyword anticipates a singular Promise object instead of an array of them.

T.J. Crowder consistently offers valuable insights: As he highlighted, a straightforward solution involves awaiting the promises from the map operation as follows:

const movingFilesJob = await this.photo.persistPhotos(this.cachedPhotoUrls, this.newDefect);

Answer №2

Reposition Promise.all within the function's code block

async persistPhotos(photoBuffer: string[], defect: Defect) {
    const name = await this.storage.get(StorageKeys.NAME);
    return Promise.all(photoBuffer.map(url => {
      const defectImage = defect.generateImageUrl(name);
      return this.file.moveImageToAppFile(url, defectImage.url);
    }));
  }

Async functions always return a single Promise. Currently, you are returning an array of Promises which results in the function returning a single Promise containing an array:

const results = await persistPhotos(...);

as a result, results will hold an array of Promises. To obtain their results, you must:

const realResults = await Promise.all(results);

Alternatively, you can reposition Promise.all inside the function.

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

The search results from the autocomplete feature of the Spotify API appear to be missing

Exploring the Spotify API - I am attempting to implement an autocompletion feature using jQuery for a field that suggests artists as users type. Here is what I have so far: HTML: <input type="text" class="text-box" placeholder="Enter Artist" id="artis ...

Encountering a Next.js application error while utilizing the button tag in conjunction with generating metadata

I keep encountering an issue with generateMetaData when trying to utilize the button tag. Can you help me resolve this problem? Currently, I am working with nextjs and I am unable to properly use the <button> tag. Whenever I implement generateMetaD ...

Guide on incorporating arrays into an array using JavaScript

Is there a way to achieve the specified outcome in JavaScript? I attempted to find a method for it on MDN but was unsuccessful. let a, b let allNumbers = [] for (a = 10; a < 60; a = a + 10) { for (b = 1; b <= 3; b++) { allNumbers.push(a ...

What is the best way to enhance an error object in Express with additional information beyond just a simple message?

I need to enhance the error handling process in my express application by passing two pieces of information to the error handler, which will then send both pieces of information in a JSON format to the client. Currently, I am only able to include an error ...

Customizing TinyMCE's font style menu options

Our platform utilizes TinyMCE as in-place editors to allow users to make live edits to content. However, a challenge arises when using a dark background with light text, as TinyMCE defaults to using this text color rather than black. (Please note: the the ...

Rails offers a unique hybrid approach that falls between Ember and traditional JavaScript responses

My current project is a standard rails application that has primarily utilized HTML without any AJAX. However, I am planning to gradually incorporate "remote" links and support for JS responses to improve the user experience. While I acknowledge that gener ...

Modify a necessary input value using jQuery or JavaScript

I am looking to update the required value of an input based on a checkbox selection. Here is my current code, any assistance would be appreciated. <input type="checkbox" id="no_land_line" name="no_land_line" value=""> // check this box if no land li ...

What is the process for launching a new terminal within Node.js?

I'm seeking advice on creating a secondary window in my node.js application where I can output text separate from the main application. Imagine having a main window for displaying information and a secondary window specifically for errors that closes ...

Is there a way to show a fallback message for unsupported video file formats?

When incorporating a video element on my webpage, I typically use the following code: <video src="some source" controls> Error message </video> Based on my knowledge, the "Error message" will only appear if the browser does not support the ...

What are the steps to integrate the vue-tweet-embed node package into vuejs2?

I am having trouble figuring out how to implement the vue-tweet-embed plugin in my Vue file. I keep getting an error message that says: Unknown custom element: - have you properly registered the component? If dealing with recursive components, ensure ...

The functionality of a Vue custom tooltip behaves strangely after clicking the button multiple times

I created this custom tooltip code that automatically closes after 2 seconds when a user clicks on a button, not just hovers over it. Initially, it works perfectly for the first two clicks, but then starts behaving strangely from the third click onwards. ...

Sending the :id parameter to the Service component

In the early days of my Angular journey, I have a simple question. Currently, I am utilizing the WordPress REST API to showcase a list of posts from a specific category by using posts?categories={ID HERE}. However, I am facing an issue in passing the ID f ...

Using Vue js and Typescript to automatically scroll to the bottom of a div whenever there are changes in

My goal is to automatically scroll to the bottom of a div whenever a new comment is added. Here's what I have been trying: gotoBottom() { let content = this.$refs.commentsWrapper; content.scrollTop = content.scrollHeight } The div containing ...

Exploring the nesting of client components in Next.jsIf you are

Exploring the realm of NextJS and React, I find myself delving into the realm of client components. One such client component I'm working with is called Form.jsx. It looks something like this: export default function FormHome() { ... a plethora of ...

Smooth-scroll plugin does not activate active state (due to JS modification)

I'm currently facing an issue with a script that handles smooth scrolling and the active state on my main navigation. The plugin in question can be found at: It's important to note that the navigation bar is fixed and therefore has no height. T ...

Mac OS reports an Illegal instruction: 4 error when running NodeJS

Each time I try to execute my program, it gives me an Illegal instruction: 4 error without any clue as to why. The code snippet in question: glob('/path/music/*.mp3', function(error, files) { for(var i = 0; i < files.length; i++) { songs ...

Triggering target selection based on the href attribute consistently executing

How can I accurately determine if an anchor tag contains only a "#" in its href attribute? var link = $('#seller-shop').attr('href'); if (link === '#') { alert('I contain #') } else { alert('I do not cont ...

Navigating Timezones with PrimeNG Calendar Component <p-calendar>

I am experiencing an issue with PrimeNG in a multi-user environment where each user is in a different timezone. I need all users to submit their form data in EST, but it seems like the browser is converting the dates to the user's local timezone upon ...

Guide on integrating a jQuery method for validating a value using regular expressions

I'm currently using the Jquery validation plugin to validate form fields. One of the fields I am validating is for academic years, which should be in the format of 2013-2014. To achieve this validation, I have created a custom Jquery method as shown b ...

What is the method for including a TabIndex property in a JSON file?

I discussed the integration of HTML fields within a JSON file and highlighted how to utilize the TabIndex property effectively in JSON files. ...