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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:58:25+00:00 2026-06-17T08:58:25+00:00

Use case: A user creates a new task, which has to be sent upstream

  • 0

Use case: A user creates a new task, which has to be sent upstream through an API. When that API returns success, I add it to the scope for display. I have that working fine with:

$http.post('some_url', newtask).success(function(data) {
  $scope.tasks.push(data);
});

(newtask is a simple object defined earlier, not shown here).

The problem is, the API is quite slow (though reliable), whereas I want this to feel like a real-time app. So I’d like to push the new task to the $scope immediately, then replace it with the “real” one when the API returns success. So I prepend the above with:

$scope.tasks.push(newtask); // Add to UI immediately (provisionally)

What happens now is that the new task is added immediately, then a second copy of the task is added when the API returns. So what I’d like to do is remove the first copy as soon as the second copy is added.

I can’t seem to find a find a way to do that. Or is my approach all wrong? (I admit, the approach does feel like a bit of a hack, so I’m open to suggestions).

  • 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-17T08:58:26+00:00Added an answer on June 17, 2026 at 8:58 am

    In the success callback function, I suggest you compare the new task (coming back from the API) to the last task in your tasks array. If they match, you don’t have to do anything. (If they don’t match, you’ll need to remove the last item and throw up some kind of error indication.)

    $http.post('some_url', newtask).success(function(data) {
       if($scope.tasks[$scope.tasks.length-1] === data) {
          // do nothing, we already added it before
       } else {
         ... error handling here
       }
    });
    

    If the user can’t add more than one task at a time (before the API returns), and you really want to remove then add, just use normal JavaScript operations on the array to remove the last item, then add the API value:

    $http.post('some_url', newtask).success(function(data) {
        $scope.tasks.pop();
        $scope.tasks.push(data);
    });
    

    Angular should notice the model change and automatically update the view for you.

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

Sidebar

Related Questions

Here's the use case: A user clicks on a link that opens a window
Given the use case where the user has to perform an online transaction, eg
In my app for a certain use case I create a new user (programmatically
Use case: user clicks the link on a webpage - boom! load of files
Use Case When a user goes to my website, they will be confronted with
Use Case Show a photo uploaded by the user in a square box with
Consider the following use case 1. User selects a product to purchase on seller's
I'm just starting to learn AspectJ, and I have use-case for say, User login.
Use case: class MyTask(Task): queue = 'default_queue' def run(self): # do work Normally I
I have a SWING UI that contains a button that creates a new SwingWorker

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.