Promises.all are on an infinite loop

Asked

Viewed 126 times

0

I have the following code segment:

        generateEpisode(result,programId).then((arrayBlocks) =>{

            const promises = []

            arrayBlocks.map((file,index) =>{
                promises.push(normalizeFile(file,index))
            });

            Promise.all(promises).then((finalFiles) =>{
                res.render("user/episodes/reviewEpisode",{script: finalFiles});
            }).catch((error) =>{
                req.flash("error_msg", error.message);
                res.redirect('/user/episodios');
            });

        }).catch((error) =>{
            req.flash("error_msg", error.message);
            res.redirect('/user/episodios');
        });

In which the function normalizeFiles() creates a new file for each file arrayBlocks, but the files are correct, but after solving them and generating the files, they begin to solve again even though they have already solved them, turning an infinite loop and never rendering the view.

function normalizeFiles:

async function normalizeFile (file,index){
    return new Promise((resolve,reject) =>{

        let newFilename = "newFIle"+(index+1)+'-'+Date.now()+".wav";

        normalize({
            input: './public/uploads/'+file,
            output: './public/uploads/result/'+newFilename,
            loudness: {
                normalization: 'ebuR128',
                target:
                {
                    input_i: -23,
                    input_lra: 7.0,
                    input_tp: -2.0
                }
            },
        }).then(() =>{
            console.log('created the file!');
            resolve(newFilename);
        }).catch((error) =>{
            console.log('error!')
            reject(error);
        });
    });
}

  • You can show the function normalizeFile?

  • I edited the post and added the normalizeFile

  • Adds return before Promise.all inside generateEpisode(...).then(

  • You can simplify by writing only const promises = arrayBlocks.map(normalizeFile); return Promise.all(promises);

  • Still it continues in loop, example, I put to print the generated files and the map files (in this case the Locks arrayBlocks had 2 files). outworking: [ Promise { <pending> }, Promise { <pending> } ] [ 'newFile 1-1568378345251.wav', 'newFile 2-1568378345258.wav' ] [ Promise { <pending> }, Promise { <pending> } ]

  • But you know if he’s going to the .catch(? and which one of them?

  • It does not stop at any catch, the console.log(finalFiles), ta inside the last .then( hence it prints the two generated files: [ 'newFile 1-1568378345251.wav', 'newFile 2-1568378345258.wav' ] and then re-print the files and generate again and again, but it never renders the view and loops all the files

Show 2 more comments

1 answer

0

When you make use of the notation async it is not necessary to return a Promisse 'cause that happens automatically. Below is a new way to arrive at the expected result, change your method normalizeFile by the content below:

async function normalizeFile (file,index){
    let newFilename = "newFIle"+(index+1)+'-'+Date.now()+".wav";

    try {
        let result = await normalize({
            input: './public/uploads/'+file,
            output: './public/uploads/result/'+newFilename,
            loudness: {
                normalization: 'ebuR128',
                target:
                {
                    input_i: -23,
                    input_lra: 7.0,
                    input_tp: -2.0
                }
            },
        });

        console.log('created the file!');
        return result;        
    } catch (e) {
        console.log('error!')
        throw e;
    }
}

Or else you can simply remove the async declared before Function and keep your code with the return of Promise.

You may be delving into Promises and seeing about async/await at MDN.

Thank you.

  • building the normalizeFiles thus, it pass straight through without going through the function and when asked to print the presses the beginnings of printing Promise { pending } he just prints undefined

  • @Anji is sure he put the await before the normalizeFiles?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.