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

  • Home
  • SEARCH
  • 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 9254337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:26:35+00:00 2026-06-18T11:26:35+00:00

I am feeling very stupid today. I keep looking at this code, trying to

  • 0

I am feeling very stupid today.
I keep looking at this code, trying to trace it, but I just cannot figure out:

  1. What it’s actually meant to do
  2. How it works

As far as I can see, the very first time it’s called it’s the only time action is the same as callFn. So, the very first time it’s run, it creates the stack array. Then I lose it. Action is assigned a function that just adds the passed callback to the stack. Then fn is actually called and based on its result, “action” is set to either callFn (?!?) or to a function that calls the callback… And then, all the calls in the stack are called.

I hate getting lost in code, but this is a little beyond me. Anybody smarter than me able to “get it”?

var memoize = function(fn) {
    var callFn = function(callback) {
        var stack = [callback];

        action = function(callback) {
            stack.push(callback);
        };

        fn(function(err, val) {
            action = err ? callFn : function(callback) {
                callback(null, val);
            };

            while (stack.length) stack.shift()(err, val);
        });
    };

    var action = callFn;

    return function(callback) {
        action(callback);
    };
};
  • 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-18T11:26:36+00:00Added an answer on June 18, 2026 at 11:26 am

    It does not only memoize the result of fn, it also ensured that there is only one active call to fn at a time. Every callback that is provided during an active call (when action is that anon function from line 5) is pushed on the stack, and once fn calls back the stack (it’s a queue actually) is processed and the result is published to all callbacks.

    The confusing “action is set to either callFn or…” happens based on the err condition. This parameter indicates that an error happened and is falsy otherwise. So when fn calls back with an error, the stack is processed as usual but also the action is reset to the original callFn (you remember, it had that value at the beginning as well) so subsequent calls will retry to get a value from fn. If there was no error, the action is just set to a function that always calls back with the result val that came back once.

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

Sidebar

Related Questions

I get the feeling that this is a very stupid question, but I cannot
I'm feeling very silly for not being able to figure this one out, but
I'm feeling stupid.This is very clear but I can not solve my problem.So excuse
I have a feeling that this question is very simple, but I just can't
I have a very bad feeling about using lock in my code but now
I have a feeling this answer is going to be very simple, but I
I am not very familiar with RoR but my colleague has written this code
I have a feeling this is something very obvious that I'm missing, but here
I'm feeling very stupid, but...what is the right way to init instance variables in
I'm new to regular expressions and i'm feeling this very difficult to solve: I

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.