Promises in the foreach loop (Typescript 2)

10,409
uploadImages(images):Promise<any> {
    let promises_array:Array<any> = [];
    for (let image of images) {
        promises_array.push(new Promise(function(resolve,reject) {
            upload(image.type_1).on('upload_completed', (data) => {
                    resolve(true);
            })
        }));
        promises_array.push(new Promise(function(resolve,reject) {
            upload(image.type_2).on('upload_completed', (data) => {
                    resolve(true);
            })
        }));
    return Promise.all(promises_array);
}
uploadImages(images).then(doSomethingElse);
Share:
10,409
dr1v3
Author by

dr1v3

Updated on June 04, 2022

Comments

  • dr1v3
    dr1v3 almost 2 years

    I've stuck with the promise chains, i need to proceed some code after uploading images to the server. It's Angular2 and Typescript. Lets write some pseudo-code:

    uploadImages(images):Promise<any> {
        return new Promise((resolve, reject) => {
            for (let image of images) {
                upload(image.type_1).on('upload_completed', (data) => {
                     // do something
                })
    
                upload(image.type_2).on('upload_completed', (data) => {
                     // do something
                })
        }
    }
    
    uploadImages(images).then(doSomethingElse);
    

    I did this task in some way like this, but i'm a bit confused with promise chains, i can't figure out how to chain this image uploads in the foreach loop and return the result it in the new promise when all uploads will be done. What is the correct way to do this?

    EDIT: There are event-based callbacks in the loop, how to convert to promises them for using Promise.all()?

    • Heretic Monkey
      Heretic Monkey over 7 years
    • dr1v3
      dr1v3 over 7 years
      @MikeMcCaughan, there are event-based callbacks in the loop
    • Heretic Monkey
      Heretic Monkey over 7 years
      So, they're not promises?
    • JB Nizet
      JB Nizet over 7 years
      Create one promise per upload (by resolving the promise in the callback), and then use Promise.all() to create a global promise out of all the individual promises.
    • dr1v3
      dr1v3 over 7 years
      @JBNizet i did it like above and got "TypeError: Cannot read property 'length' of undefined" error
    • Heretic Monkey
      Heretic Monkey over 7 years
      You have to initialize the array. That probably will won't work, since promises_array won't have anything in it when it gets to all... Might want to look at stackoverflow.com/q/22519784/215552
    • dr1v3
      dr1v3 over 7 years
      @MikeMcCaughan, yep, sorry, i made a mistake in the first code version
    • dr1v3
      dr1v3 over 7 years
      @MikeMcCaughan Thanks a lot!
    • dr1v3
      dr1v3 over 7 years
      @JBNizet Thanks a lot!
    • Heretic Monkey
      Heretic Monkey over 7 years
      @dr1v3 Feel free to post that solution as an answer. We don't like answers in the questions here, and we encourage people to answer their own questions when they've figured out themselves.