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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:31:18+00:00 2026-06-17T19:31:18+00:00

I understand that for I/O operations(ie database queries, web requests and disk access) you

  • 0

I understand that for I/O operations(ie database queries, web requests and disk access) you must use callbacks like this

fs = require('fs')
fs.readFile('test.txt', 'utf8', function (err,data) {
  if (err) {
    return console.log(err);
  }
  console.log(data);
});

but say if you have synchronize code like this

function Player(name){
    this.name = name;
    this.score = 0;
}
Player.prototype.calcScore = function(){
    //some special code here to calculate the score
    this.score =+ 10;
}
var player = new Player("Sam");
player.calcScore();
console.log(player);

Or do you need to write it in a callback style like below where the calcScore method only includes maybe a for loop and an if statement and does not query databases etc.

function Player(name){
    this.name = name;
    this.score = 0;
}

Player.prototype.setScore = function(data){
    this.score = data
}

Player.prototype.calcScore = function(callback){
    //some special code here to calculate the score
    var newscore = this.score += 10;
    callback(null, newscore);
}

var player = new Player("Sam");

player.calcScore(function(err, data){
    if(err){
        return console.log(err);
    }
    player.setScore(data);
    console.log(player);
});

I guess I am a bit confused and about when to use async code or sync code. Thanks in advance for your help.

  • 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-17T19:31:19+00:00Added an answer on June 17, 2026 at 7:31 pm

    You don’t need to set up an asynchronous callback when all you’re doing is JavaScript statements. Asynchronous APIs are used when your code is dealing with the “real world”. When you access IO devices, or the network, asynchronous APIs are used because there are unpredictable delays inherent in the activities.

    If you’re doing a lot of computation, there are ways of setting up a “continuation” model that allows for periodic interruption of the work, but that’s not really the same thing.

    edit — a comment wisely points out that there’s really no harm in designing a subsystem with asynchronous APIs even if it’s not really necessary. I’ll also note that designing with callbacks is not something that’s done only for asynchronous mechanisms. There are good reasons to exploit the flexibility of JavaScript functions-as-values.

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

Sidebar

Related Questions

I have a web service that exposes operations to and from a database. I'm
I understand that this question may be subjective, this is why I need an
I understand that in CUDA's memory hierachy, we have things like shared memory, texture
I have an application where I'd like to use a NoSQL database, but I
I am using JDBC to retrieve data from a database. I understand that a
I understand that a plugin registered for pre-validation executes outside of the database transaction
I've seen this question posted here previously but I'm not satisfied that I understand
I've found that tooltip text in jsyntaxpane's find/replace operation. However, I don't understand what
I understand that: '\n' // literally the backslash character followed by the character for
I understand that JVM and CLR were designed as stack-based virtual machines. When JIT

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.