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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:48:45+00:00 2026-06-13T09:48:45+00:00

I write lots of modules which look like this: function get(index, callback) { if

  • 0

I write lots of modules which look like this:

function get(index, callback) {
    if (cache[index] === null) {
        request(index, callback); // Queries database to get data.
    } else {
        callback(cache[index]);
    }
}

Note: it’s a bit simplified version of my actual code.

That callback is either called in the same execution or some time later. This means users of the module aren’t sure which code is run first.

My observation is that such module reintroduces some problems of the multi-threading which was previously solved by JavaScript engine.

Question: should I use process.nextTick or ensure it’s safe for the callback to be called outside the module?

  • 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-13T09:48:46+00:00Added an answer on June 13, 2026 at 9:48 am

    It depends entirely on what you do in the callback function. If you need to be sure the callback hasn’t fired yet when get returns, you will need the process.nextTick flow; in many cases you don’t care when the callback fires, so you don’t need to delay its execution. It is impossible to give a definitive answer that will apply in all situations; it should be safe to always defer the callback to the next tick, but it will probably be a bit less efficient that way, so it is a tradeoff.

    The only situation I can think of where you will need to defer the callback for the next tick is if you actually need to set something up for it after the call to get but before the call to callback. This is perhaps a rare situation that also might indicate a need for improvement in the actual control flow; you should not be rely at all on when exactly your callback is called, so whatever environment it uses should already be set up at the point where get is called.

    There are situations in event-based control flow (as opposed to callback-based), where you might need to defer the actual event firing. For example:

    function doSomething() {
        var emitter = new EventEmitter();
        cached = findCachedResultSomehow();
        if (cached) {
            process.nextTick(function() {
                emitter.emit('done', cached);
            });
        } else {
            asyncGetResult(function(result) {
                emitter.emit('done', result);
            });
        }
        return emitter;
    }
    

    In this case, you will need to defer the emit in the case of a cached value, because otherwise the event will be emitted before the caller of doSomething has had the chance to attach a listener. You don’t generally have this consideration when using callbacks.

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

Sidebar

Related Questions

I have a project which has lots of modules, each one has different running
I have a config file, named config.txt, look like this. IP=192.168.1.145 PORT=10022 URL=http://www.stackoverflow.com I
I write lots of little libraries and modules and usually these libraries and modules
Like lots of you guys on SO, I often write in several languages. And
I'm trying to write a script which basically takes an array of lots of
In the application I am writing, I need to write lots of base types,
I am generating lots of images in java and saving them through the ImageIO.write
My code looks close to this: Lots of writes using normal C++ IO stream
Write a NASM macro: divide, which has 2 arguments, which specify unsigned integers in
Write a NASM macro: divide, which has 2 arguments, which specify unsigned integers in

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.