2

I have two collections of products and categories and I have both collections that have data products(152), categories(10). So, I tried to connect the DB to retrieve the data. First I call products collection data next call categories collection data using async-await functionality. But it gets the first categories of data and next product data. How to solve this issue anyone can give the answer.

product.js

async function product_data(collection) {
   let mongodb = await MongoDB.connect(collection)
   let result = await mongodb.findAll()
   return result
}

module.exports.product_data = product_data

category.js

async function category_data(collection) {
   let mongodb = await MongoDB.connect(collection)
   let result = await mongodb.findAll()
   return result
}
module.exports.category_data = category_data

app.js

const {product_data} = require("./product")
const {category_data} = require("./category")   

async function updatedb() {
    let product_data = await product_data("ecomm_product")
    console.log(product_data)
    let category_data = await category_data("ecomm_category")
    console.log(category_data)  
}

I got result

Its first print category_data after print product_data

Expected result

Its first print product_data after print category_data

saulotoledo
  • 1,737
  • 3
  • 19
  • 36
hari prasanth
  • 716
  • 1
  • 15
  • 35
  • 1
    I think first I would ask why you need them in a particular order? Notwithstanding that, essentially they are just promises and as soon as the promise is 'resolved' it will return the result. You cannot really control the order unless you chain them, so once the `product_data` returns then run the `category_data` – Intellidroid Aug 16 '19 at 12:35
  • because in my case product_data is mapped to category_data that only I want first all product inserted in DB. – hari prasanth Aug 16 '19 at 13:01
  • Could you paste your output? Seems No problem. But suspect to something MongoDB connection cause issue. Just to curious `connect` method expect MongoDB URL, not collection name. – Hardik Shah Aug 16 '19 at 13:13
  • Does this answer your question? [How to execute promises sequentially, passing the parameters from an array?](https://stackoverflow.com/questions/43082934/how-to-execute-promises-sequentially-passing-the-parameters-from-an-array) – saulotoledo May 19 '20 at 12:21
  • Your code seems to think it's Python, so that's something you probably also want to address: by convention, function and variables use lowerCamelCase in JS (not python's lower_snake_case) with functions named for what they do, and variables name for what's "in" them. Also on an [mcve] note: there is no reason to show this as three separate codeblocks: this can be presented as a single block without any problem. – Mike 'Pomax' Kamermans May 19 '20 at 23:50
  • @Intellidroid except `await` changes that, halting local execution until the Promise resolves. – Mike 'Pomax' Kamermans May 19 '20 at 23:52

1 Answers1

0

I can't reproduce this at all, not even with explicit delays in which products take longer to resolve than categories. Collapsing your code to a single file, and using proper JS conventions for naming and case:

function getProductData(collection) {
  return new Promise(resolve => {
      setTimeout(() => resolve('product'), 2000);
  });
}

function getCategoryData(collection) {
  return new Promise(resolve => {
      setTimeout(() => resolve('category'), 1000);
  });
}

async function updatedb() {
    let product_data = await getProductData("ecomm_product")
    console.log(product_data)
    let category_data = await getCategoryData("ecomm_category")
    console.log(category_data)
}

updatedb();

This simply yields the following output, every time:

$node test.js
product
category
Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153