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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:09:38+00:00 2026-06-14T09:09:38+00:00

I am confused with how to write decent code when using a lot of

  • 0

I am confused with how to write decent code when using a lot of asynchronous code.

In the following code snippet I log in to get the authentication cookie and use that cookie for the next request to get a list of projects name (as an example):

def self.populateProjectsTable(projects_controller)
  payload = {email: "email", password: "pass"}
  HTTP.post("http://example.com/login", {payload: payload}) do |response|
    authCookie = response.headers['Set-Cookie']
    HTTP.get("http://example.com/projects.json", {cookie: authCookie}) do |response|
      projects = JSON.parse(response.body.to_str)
      projects_controller.projects = projects
      projects_controller.reloadData
    end
  end
end

While this will work the code feels dirty. Not really following the single responsibility principle. I would like to extract this in a few methods:

def self.populateProjectsTable(projects_controller)
  @taskList = TaskList.new
  @taskList.doLogin
  projects = @taskList.getProjects
  projects_controller.projects = projects
  projects_controller.reloadData
end

def doLogin
  payload = {email: "email", password: "pass"}
  HTTP.post("http://example.com/login", {payload: payload}) do |response|
    @authCookie = response.headers['Set-Cookie']
  end
end

def getProjects
  HTTP.get("http://example.com/projects.json", {cookie: @authCookie}) do |response|
    projects = JSON.parse(response.body.to_str)
  end
end

This obviously does not work. The getProjects method is called before doLogin is finished and the projects are only known in the scope of the block, not giving back the data to the populateProjectsTable method.

How does one program such applications without the nesting shown in the first example?

  • 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-14T09:09:40+00:00Added an answer on June 14, 2026 at 9:09 am

    You’re not going to totally get away from the nesting. Taking Alan’s answer and massaging it a bit, this is what I’ve come up with. It involves passing a block through a couple of methods.

    def self.populateProjectsTable(projects_controller)
      @taskList = TaskList.new
      @taskList.loginAndGetProjects do |projects|
        projects_controller.projects = projects
        projects_controller.reloadData
      end
    end
    
    def loginAndGetProjects(&block)
      payload = {email: "email", password: "pass"}
      HTTP.post("http://example.com/login", {payload: payload}) do |response|
        @authCookie = response.headers['Set-Cookie']
        getProjects(&block)
      end
    end
    
    def getProjects(&block)
      HTTP.get("http://example.com/projects.json", {cookie: @authCookie}) do |response|
        projects = JSON.parse(response.body.to_str)
        block.call(projects)
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write this loop in Python but get confused. Basically I
Confused by java compilation process OK i know this: We write java source code,
I'am a little bit confused what is better to use debug or write unit
I'm a bit confused about the right way to write a code. I read
I'm confused on how to write the following query in MySQL: Given one collaborator,
I'm trying to write a parser using flex and bison but I'm confused on
I am so confused right now. I am supposed to write a program that
I am really confused how to write query for the following statement. How to
Getting extremely confused with an adminhtml module i'm trying to write! Effectively I have
I'm confused as hell with all the bazillion ways to read/write/create excel files. VSTO,

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.