Anticipating the completion of post requests

I am currently working on implementing a file upload feature in Angular. I have tackled the issue of file size restrictions by creating an API endpoint that can receive file chunks. Once all the chunks are received, another endpoint needs to be triggered to combine these chunks on the server. The challenge I am facing is determining when all the post requests are complete without relying on Promises. Since I am new to Angular and JavaScript, I am unsure how to approach this problem.

let i = 0;
for(let offset = 0; offset < file.size; offset += chunkSize) {
  let chunk = file.slice( offset, offset + chunkSize );
  let formData = new FormData();
  formData.append("fileUpload", chunk, file.name + ".part" + i);
  formData.append("identifier", identifier.toString());
  this.http.post(this.baseUrl + "Upload", formData).subscribe();
}

Answer №1

To store these in an array and make use of forkJoin, then you can subscribe to the result.

This approach allows all the http requests to be made simultaneously, with the subscribe function executed once they are all finished.

let count = 0;
const requestArray = [];
for( let start = 0; start < data.length; start += batchLength ){
    let batch = data.slice( start, start + batchLength );

    let formData = new FormData();
      formData.append("dataBulk", batch, data.name + ".batch" + count);
      formData.append("refID", referenceId.toString());
    requestArray.push(
      this.http.post(this.apiUrl + "BatchUpload", formData)
    ).
}
forkJoin(requestArray).subscribe();

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

Set up local npm packages for easy access by different projects

Can someone explain to me how npm works compared to Maven (I have a background in Java) when it comes to package management? I've developed a generic component using Angular 4 that will be used across multiple projects. I've published it to our n ...

Array of options with specified data types in Props interface

Trying to implement options as props for styling a button component in Astro. Still learning TypeScript. Encountering the error message: Generic type 'Props<style>' requires 1 type argument(s). Below is the code snippet: --- import type { H ...

Provide the context for a template within a dynamically generated component

I have been working on creating a component similar to a tooltip control, but I am facing an issue. The current setup only supports simple text in the ng-template, and when attempting to pass a more complex template with bindings, it breaks. To address thi ...

Is JavaScript Promise Chaining Allowed?

I have a question regarding my code, despite it currently functioning correctly. Specifically, I'm wondering if the sequence of promises in my database is valid. Promise 1 must be fulfilled before moving on to Promise 2 because I rely on the data and ...

Troubleshooting the "Failed to mount component" error in Vue: fixing template or render function definition issues

Struggling with writing a Vue component, encountering the issue: Failed to mount component: template or render function not defined. Tried various fixes like adding default when importing the component, but none of them seem to work. My component code is i ...

Exploring Node.js and JSON: Retrieving specific object attributes

Utilizing ExpressJS, NodeJS, and Bookshelf.js In an attempt to display a user's list of friends, encountering the error "Unhandled rejection TypeError: Cannot read property 'friends' of undefined" when trying to access a property of the obj ...

Issue with external JavaScript file being unresponsive on mobile browser

Hello everyone, hope you're having a great afternoon or evening I've encountered an issue with mobile browsers like Chrome Mobile and Kiwi where the external js file is not visible in the html file. The script.js file looks like this: alert(&ap ...

What is the best way to transfer data from an Ajax function to a controller action?

I have a button in my view that triggers a jQuery Ajax function, with parameters fetched from my model <input type="button" value="Run Check" onclick="runCheck('@actionItem.StepID', '@Model.Client.DatabaseConnectionString', '@M ...

Avian-themed masking feature for jCarousel

Currently, I have a series of images in constant motion using jCarousel. Only one image is visible fully at any given time, and I am looking to create a "feathering" effect on the edges of the carousel so that the images smoothly fade in and out as they co ...

Verify that the zip code provided in the input matches a record in the JSON data and extract the

I need to create a feature where users can input their zip code, check if it matches any of the zones in a JSON element, and then display the corresponding zone: var zones = [{ "zone": "one", "zipcodes": ["69122", "69125", "69128", "69129"] }, ...

How to Use JQuery to Retrieve the Nearest TD Element's Text Content

Hey there, here is some HTML code that I need help with: <tbody> <tr> <td class="text-center"><input type="text" class="form-control cardSerialNumber"></td> <td class="text-center"><button type="button" onc ...

What steps do I need to take to create a fresh interface in useState with the help of Typescript

I'm attempting to replicate an input by utilizing useState with an interface. Each time I click on the + button, the interface should be duplicated in the state, thereby duplicating my input. Here is the code I am working on: interface newInputsInter ...

Every time I rotate my div, its position changes and it never goes back to

Trying to create an eye test by rotating the letter "E" when a directional button is clicked. However, facing an issue where the "E" changes position after the initial click instead of staying in place. Here is a GIF showcasing the problem: https://i.stac ...

"Transitioning from jQuery to Vanilla Javascript: Mastering Scroll Animations

I'm seeking guidance on how to convert this jQuery code into pure Javascript. $('.revealedBox').each(function() { if ($(window).scrollTop() + $(window).height() > $(this).offset().top + $(this).outerHeight()) { $(this).addCla ...

Delete items from several arrays on a click event in React

I'm working with an array of objects that contain multiple arrays. My goal is to remove the item when a button is clicked, but I keep getting undefined as a result. JSON Data [ { "services": [ { "id": "1b9 ...

Web scraping with Cheerio in Node.js sometimes yields undefined results

When attempting to extract data from NSE website, I initially tried using the inspect element console: (Edited the question) objs = $('div[class="table-wrap"] > table > tbody > tr > td').slice(0, 8) objs.map((i,element) =& ...

Arranging JSON array in Angular 6 by specific key within the nested array

Is there a way to sort the data object based on the order numbers inside the array objects? data =[ { name:'' list:{ order :2 }, { name:'' list:{ order :1 } ] ...

Choose up to three elements using jQuery

DEMO I'm currently working on a project where I need to select only 3 items at a time. However, all the elements are being selected instead. Can someone please provide guidance on how to achieve this? "If a user wants to select another element, th ...

Efforts to toggle visibility of icons in React

One issue I encountered is with the Navbar in mobile mode, where the icons are covering the menu. Refer to the image for a visual representation of the problem. To address this issue, my plan is to hide the icons when the hamburger icon is clicked. Below ...

ts:Accessing the state within a Redux store

In my rootReducer, I have a collection of normal reducers and slice reducers. To access the state inside these reducers, I am using the useSelector hook. Here is my store setup : const store = configureStore({reducer : rootReducer}); Main Reducer: const ...