Skip to content

Running two promise with identic query somehow went wrong #1368

Closed

Description

@niwasmala

Problem
I have a google spreadsheet and want to load several sheets in paralel. But the result is not working as expected.
Imagine the google sheet have 2 sheets, categories and products.

Google Sheets
https://docs.google.com/spreadsheets/d/1iN8IsyPAJe8nFlBUvjEu1UX6mvxOORLIaG8CIyHkFGU/edit?usp=sharing

async function loadCategory() {
  const url = 'https://docs.google.com/spreadsheets/d/1iN8IsyPAJe8nFlBUvjEu1UX6mvxOORLIaG8CIyHkFGU/gviz/tq?tqx=out:csv&gid=976108113';
  const res = await alasql.promise('SELECT * FROM CSV(?, {headers:true})', [url]);
  
  console.log('category', res[0]);
  
  return res;
}

async function loadProduct() {
  const url = 'https://docs.google.com/spreadsheets/d/1iN8IsyPAJe8nFlBUvjEu1UX6mvxOORLIaG8CIyHkFGU/gviz/tq?tqx=out:csv&gid=1160488713';
  const res = await alasql.promise('SELECT * FROM CSV(?, {headers:true})', [url]);
  
  console.log('product', res[0]);
  
  return res;
}

loadCategory();
loadProduct();

I'm expecting the console log would be like this

image

But I got

image
which the result of category goes to loadProduct and no response from loadCategory

Please notice that the query are identic both in loadProduct and load Category, only the parameters are different

const res = await alasql.promise('SELECT * FROM CSV(?, {headers:true})', [url]);

When I change the query so they are not identic, e.g adding alias

async function loadCategoryWithAlias(){
  const res = await alasql.promise('SELECT * FROM CSV(?, {headers:true}) AS `Category`', [url]);
  ...
}

async function loadProductWithAlias(){
  const res = await alasql.promise('SELECT * FROM CSV(?, {headers:true}) AS `Product`', [url]);
  ...
}

It works as expected.

Code Replication
https://jsfiddle.net/rsukb37o/

You can try to run it several times, and the results would be different.

Activity

  1. mathiasrw commented on Oct 1, 2021

    @mathiasrw
    Member

    Your async function never returns because the scripts reaches the end and kills the process.

    You need to await the answer.

    To await the answer the need to run it inside a function. The following should do the trick, but I did not test it

    (async ()=>{
    	  async function loadCategory() {
    	    const url = 'https://docs.google.com/spreadsheets/d/1iN8IsyPAJe8nFlBUvjEu1UX6mvxOORLIaG8CIyHkFGU/gviz/tq?tqx=out:csv&gid=976108113';
    	    const res = await alasql.promise('SELECT * FROM CSV(?, {headers:true})', [url]);
    	    
    	    console.log('category', res[0]);
    	    
    	    return res;
    	  }
    	  
    	  async function loadProduct() {
    	    const url = 'https://docs.google.com/spreadsheets/d/1iN8IsyPAJe8nFlBUvjEu1UX6mvxOORLIaG8CIyHkFGU/gviz/tq?tqx=out:csv&gid=1160488713';
    	    const res = await alasql.promise('SELECT * FROM CSV(?, {headers:true})', [url]);
    	    
    	    console.log('product', res[0]);
    	    
    	    return res;
    	  }
    	  
    	  await loadCategory();
    	  await loadProduct();
    })()

    or you might want to do it a bit faster by initiating the requests with a await Promise.all(loadCategory(), loadProduct())

    Let me know if it works.

  2. niwasmala commented on Nov 19, 2021

    @niwasmala
    Author

    Sorry for the late reply. Yep, it works! My bad for missing the promise and not waiting for the answer.

    Thanks! 👍

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions