0

I'm doing a series of file uploads using HTML5 API and I want to run a collection of Promises in succession and only do a final PUT when all files are uploaded.

Currently, this is what I have:

obj.attachments.forEach( (file) => {                                                              
  const request = fetch(`${window.location.origin}/api/cases/${obj.id}/attachment`, {             
    ...baseSettings,                                                                               
    body: file,                                                                                    
    headers: {},                                                                                   
  }).then((req) => req.json())                                                                     
    .then((json) => { console.log('upload ', json); });                                            
});                                                                                                


const request = fetch(`${window.location.origin}/api/cases`, putJSON(_.omit(obj, 'attachments')));
request.then((req) => req.json())                                                                  
       .then((json) => dispatch(receiveCase(json)));                                               

Ideally, the obj.attachments would be transformed into a collection of Promises and the final fetch could be appended and they would all run in succession.

Jon Lebensold
  • 339
  • 3
  • 10

1 Answers1

1

thanks to Jonah, here's my solution:

return Promise.all(kase.attachments.map( (file) => {                                                  
  return fetch(`${window.location.origin}/api/cases/${kase.id}/attachment`, {                         
    ...baseSettings,                                                                                  
    body: file,                                                                                       
    headers: {},                                                                                      
  }).then((req) => req.json()).then( (json) => {                                                      
    console.log(json, file);                                                                          
  });                                                                                                 
})).then( files => {                                                                                  
  const request = fetch(`${window.location.origin}/api/cases`, putJSON(_.omit(kase, 'attachments'))); 
  return request                                                                                      
    .then((req) => req.json())                                                                        
    .then((json) => dispatch(receiveCase(json)));                                                     
});                                                                                                   
Jon Lebensold
  • 339
  • 3
  • 10