Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8110581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:48:14+00:00 2026-06-06T01:48:14+00:00

I have an script that adds products to a remote database using a RESTful

  • 0

I have an script that adds products to a remote database using a RESTful API in Node.js. It works well, but I would like to change the priority HTTP requests are processed. Here is some code to better illustrate what I am trying to do:

step(
    function initializeCategories() {
        createCategories(products, this);
    },
    function createProducts(err, categoriesHash) {
        console.log("\nCreating products:");
        console.log("==================");

        var group = this.group(),
            productDoneCallback;

        products.forEach(function (product) {
            product.categories = categoriesHash[product.category + "/" + product.make + "/" + product.model];
            productDoneCallback = group();

            step(
                function createProduct() {
                    postProduct(convertToBigCommerceObj(product), this);
                },
                function overwriteProduct(err, product, allowOverwrite) {
                    if (err) {
                        console.log(err);
                    }

                    allowOverwrite = allowOverwrite || false;

                    if (allowOverwrite) {
                        updateProduct(product, this);
                    } else {
                        this(err, product);
                    }
                },
                function addExtraInfo(err, product) {
                    addImage(product, productDoneCallback);
                }
            );
        });
    },
    function printStats(err) {
        if (err) {
            logError(err);
        }

        var endTime = +new Date(),
            duration = endTime - startTime;

        console.log("\nFinished after " + (duration / 1000 / 60) + " minutes");
        console.log(productsAdded + " Products added successfully");
        console.log(productsUpdated + " Products updated successfully");
        console.log(productsSkipped + " Products skipped");
        console.log("Average time (milliseconds) per product was : " + (duration / totalNumProducts ));
        console.log("For more information see error log (error.log)" );
    }
);

In this code the product images are always added last after all products have been added. This is because the forEach loop puts all of the postProduct requests on the node event queue right away. After the first product is done has been posted to the server another entry is added to the end of the queue to add that product’s image. Instead, I would like that new entry to float to the top of the queue and be the next entry to be processed (not another product post, that can wait).

I realize that to do that I need a priority queue. I am just not sure how I can achieve this in Node and Javascript.

Update: after finding an implementation of a PriorityQueue at https://github.com/STRd6/PriorityQueue.js, the real trouble is processing the queue asynchronously every time a request completes. Or more like everytime there is an available http channel that has been freed we need to take the highest priority item.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T01:48:18+00:00Added an answer on June 6, 2026 at 1:48 am

    Whenever you heard “priority queue”, you should think about having a heap data structure. They are not the only way to do that, but they are simple to implement and so are a good first goto.

    See http://eloquentjavascript.net/appendix2.html for a random implementation. They assume that when you create the heap, you pass in a function that takes an element and returns its priority. In your case you could store elements like [priority, object] and initialize your heap wth a function like function (x) {return x[0]}.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that adds images to my products. It is used to
I have been working on a script that pulls testimonials from a database, adds
I have a problem with using a script that adds a NiceEdit toolbar to
I have a JS/jQuery script that adds our leads (web contacts) to the DOM
I have a script that connects to SQL Server 2005 default instance. But I'm
I have a script that generates images from text using PHP. It's working fine
I have found a script that adds mouse trail on Opera's mouse gesture. Very
I have a script that loops and adds markers one at a time. I
I have a user script (for chrome and FF) that adds significant functionality to
I have a script that adds all children from the current clicked element to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.