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 7039267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:46:36+00:00 2026-05-28T01:46:36+00:00

I am trying to implement a simple queue for functions that take callbacks. My

  • 0

I am trying to implement a simple queue for functions that take callbacks. My problem is that in the flush method, “this” is null when the action being performed is some long-running thing (in my case, indexedDB calls). I’ve never experienced this sort of behavior before, so please educate me on what is going on?

Here’s the code:

var Queue = (function () {
    function Queue() {
    };

    Queue.prototype.items = [];
    Queue.prototype.results = [];
    Queue.prototype.add = function (action) {
        this.items.push(action);
    };
    Queue.prototype.complete = function () { };
    Queue.prototype.flush = function () {
        var args = Array.prototype.slice.call(arguments);
        if (args.length > 0) { this.results.push(args); }
        if (this.items.length > 0) {
            var action = this.items.shift();
            action.call(this);
        } else { // Complete, call back multi.
            var results = this.results;
            this.clear();
            this.complete(results);
        }
    };
    Queue.prototype.clear = function () {
        this.items = [];
        this.results = [];
    };

    Queue.create = function () {
        return new Queue;
    };

    return Queue;
})();
  • 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-05-28T01:46:37+00:00Added an answer on May 28, 2026 at 1:46 am

    My best guess, based on what you’ve given us, is that you’re passing a reference to the Queue.prototype.flush function object to some asynchronous function so it can be used as a callback. If that’s true, flush() is called like this:

    function ayncFunction(callback) {
        // Do some stuff
        callback();
    }
    ayncFunction(myQueue.flush);
    

    … instead of like this:

    myQueue.flush();
    

    Because the function is called separately, instead of on an object, this will be set window (unless you’re using strict mode, in which case it’s undefined).

    You can circumvent the problem by wrapping the call to myQueue.flush() in a function, like this:

    var myCallback = function() {
        myQueue.flush();
    };
    ayncFunction(myCallback);
    

    This works because myCallback becomes a closure around myQueue.

    Another way to solve the problem is to use bind() to tie the function to its context (i.e. the object on which it should be called).

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

Sidebar

Related Questions

I'm trying to implement a simple queue that performs one task at a time.
I'm trying to implement a simple queue that performs one task at a time.
I'm trying to implement a simple horizontal navigation menu that just shows a single
I'm trying to implement a simple priority queue from array of queues. I'm trying
I am trying to implement simple xhr abstraction, and am getting this warning when
I'm trying to implement a simple method to read new lines from a log
I was trying to implement a simple function that can concatenate any number of
I'm trying to implement simple Event Bus I started like this: public class RegistrationData
I'm trying to implement simple Comet chat example and for this I implemented Long
I was trying to implement a simple queue in C programming. But I encountered

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.