1

I have the following scenario:

savePicture(newPicture, fileData){
    return compressionUtil.makePictureAndThumbnail(newPicture).then((data) => {
        let writeBig = fsUtil.writeFileToDisk(pictureFileName, data[0])
        let writeSmall = fsUtil.writeFileToDisk(thumbnailFileName, data[1])

        return Promise.all([writeBig, writeSmall]).then(() => {
            let picToSave = {
                uploaderUsername: fileData.username,
                directory: fileDirectory,
                fileName: fileName,
                tags: fileData.tags,
                description: fileData.description,
                droneTaken: fileData.droneTaken,
                isGenuine: isGenuine,
                metadata: metadata
            }
            return Picture.create(picToSave)
        })
    })
}

What I am looking to do is handle possible promise rejections of compressionUtil.makePictureAndThumbnail() and Promise.all([writeBig, writeSmall]) like so

savePicture(newPicture, fileData){
    return compressionUtil.makePictureAndThumbnail(newPicture).then((data) => {
        let writeBig = fsUtil.writeFileToDisk(pictureFileName, data[0])
        let writeSmall = fsUtil.writeFileToDisk(thumbnailFileName, data[1])

        return Promise.all([writeBig, writeSmall]).then(() => {
            let picToSave = {
                uploaderUsername: fileData.username,
                directory: fileDirectory,
                fileName: fileName,
                tags: fileData.tags,
                description: fileData.description,
                droneTaken: fileData.droneTaken,
                isGenuine: isGenuine,
                metadata: metadata
            }
            return Picture.create(picToSave)
        }).catch((err)=>{/*Handle promise.all error here*/})
    }).catch((err)=>{/*Handle makePictureAndThumbnail error here*/})
}

but at the same time make savePicture() return only Picture.create() 's promise .then() and .catch().

What I have thought about is wrapping it all in a giant new Promise((resolve,reject)=>{}) but probably that's a terrible practice.

Input on that is appreciated. Thanks.

Biser A.
  • 154
  • 1
  • 13
  • Hi, you should try to present a MWE (http://stackoverflow.com/help/mcve) to make your question more accessible and increase your chance of getting an answer. – woshilapin Mar 18 '17 at 17:08

1 Answers1

0

wrapping it all in a giant new Promise((resolve,reject)=>{}) is probably a terrible practice.

Yes indeed it is!

To handle rejections only from the promise and not the then fulfillment callback, you can use .then(…, …) instead of .then(…).catch(…):

savePicture(newPicture, fileData){
    return compressionUtil.makePictureAndThumbnail(newPicture).then((data) => {
        …
        return …
    }, err => {
//   ^ no `catch`
        /* Handle makePictureAndThumbnail error here */
    });
}

Similarly,

return Promise.all([writeBig, writeSmall]).then(() => {
    let picToSave = { … }
    return Picture.create(picToSave);
}).catch(err => {
    /* Handle all errors from writeBig, writeSmall and create */
});

return Promise.all([writeBig, writeSmall]).then(() => {
    let picToSave = { … }
    return Picture.create(picToSave).catch(err => {
        /* Handle errors from create */
    });
}, err => {
    /* Handle all errors from writeBig and writeSmall */
});
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375