I want to give the user the ability to upload multiple files, each file will be sent as a separate request, after all the requests have been completed, you need to execute the request to receive all the items.
To do this, made the appropriate changes to your code, decided to check the execution of all requests through Promise.all
.then((item, attachments) => { let promises = []; attachments.forEach(attachment => { promises.push(addItem(item, attachment)); }); return Promise.all(promises.map(promise => { promise.catch(e => console.log(e)); })); }) .then(() => { return getIems(); }) .then(items => $scope.items = items); But, this code does not work, the request for receiving the items goes to the server before all requests are executed.
The method on the client to send the file to the server looks like this:
function addItem(item, attachment){ let url = ''; return Upload.upload({ url: url, data: {attachment: attachment, model: Upload.json(item) } }); } where for Upload , third-party library.
On the server side, the receiving file method looks like this:
public async Task<IHttpActionResult> AddItem() { try { //Здесь логика проверки и сохранения файла return Ok(); } catch { return BadRequest } } Tell me what am I doing wrong?
promises.map(promise => { promise.catch(e => console.log(e)); })you have an empty array returned. Addreturn promise; - Stepan Kasyanenko