Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promise.all error #1953

Open
jzzqwhw opened this issue Aug 2, 2024 · 2 comments
Open

Promise.all error #1953

jzzqwhw opened this issue Aug 2, 2024 · 2 comments

Comments

@jzzqwhw
Copy link

jzzqwhw commented Aug 2, 2024

(async () =>{
        async function fun1(){
            let f1 = "D:\\a1.xlsx"
            const res1 = await alasql.promise(`select * from xlsx(?) limit 100000`, [f1])
            return res1
        }

        async function fun2(){
            let f2 = "D:\\a2.xlsx"
            const res2 = await alasql.promise(`select * from xlsx(?) limit 100001`, [f2])
            return res2
        }

        try{
            const orderData = await Promise.all([fun1(), fun2()])    
            console.log(orderData)
        }catch(error){
            console.error(error);
        }finally{
            console.log('end');
        }
    })() 

I have an issue where I've created two asynchronous functions, fun1 and fun2, and I want to execute them in batch using Promise.all, passing parameters f1 and f2 to the xlsx(?) placeholder.

When running these functions individually, the query results are correct.

However, when I put them into Promise.all, no results are returned, and no errors are thrown.

After repeated testing, I found that if there is nothing after xlsx(?) in Promise.all, no results are returned. But if I add a limit and the number after limit is different for each function, the correct results are obtained. Yet, if the numbers after limit are the same, no results are returned again.

For example, in the code above, if both are limit 1000, no results are returned. But if one is limit 1000 and the other is limit 1001, the correct results can be obtained.

What could be the reason for this?

@mathiasrw
Copy link
Member

mathiasrw commented Aug 4, 2024

Hmmm. Very strange. I am wondering how the timing of these two functions might affect each other. Hmmmmmmm...

Im wondering if running async commands via a queue instead will affect this. It will effectively make multiple queries run in sequense, but getting incorrect results is worse.

@znenuko
Copy link

znenuko commented Aug 21, 2024

Hi,

I reviewed the issue you reported and made some adjustments to the code to address it. Here’s the updated solution:

(async () => {
    async function fun1() {
        let f1 = "D:\\a1.xlsx";
        try {
            console.log('Running fun1 with', f1);
            const res1 = await alasql.promise(`select * from xlsx(?) limit 100000`, [f1]);
            console.log('Result from fun1:', res1);
            return res1;
        } catch (error) {
            console.error('Error in fun1:', error);
            throw error; // Re-throw to be caught in the main try-catch block
        }
    }

    async function fun2() {
        let f2 = "D:\\a2.xlsx";
        try {
            console.log('Running fun2 with', f2);
            const res2 = await alasql.promise(`select * from xlsx(?) limit 100001`, [f2]);
            console.log('Result from fun2:', res2);
            return res2;
        } catch (error) {
            console.error('Error in fun2:', error);
            throw error; // Re-throw to be caught in the main try-catch block
        }
    }

    try {
        console.log('Starting batch execution');
        const orderData = await Promise.all([fun1(), fun2()]);
        console.log('Batch results:', orderData);
    } catch (error) {
        console.error('Error in Promise.all:', error);
    } finally {
        console.log('End');
    }
})();

Explanation:

Main File: The updated code performs queries on Excel files with different limits in parallel.
Error Handling: Added additional logging to detect and handle errors during execution.
Testing: Make sure to test the functions individually and adjust limits if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants