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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:31:38+00:00 2026-06-06T00:31:38+00:00

So I was asked this at an interview, but it brought up a good

  • 0

So I was asked this at an interview, but it brought up a good use case. Assume that you have a bunch of data sources. You want to find the first available one and process it and ignore the rest.

So something like:

var datasources = new Array("somedatabase1/pizza","somedatabase2/beer","somedatabase3/llama");
var dfds = new Array();
$.each(datasources,function(source){
    dfds.push($.getJSON(source));
});

$.when(dfds).done(function(){alert("they are all done");});

Ignore that I really don’t think when accepts an array (maybe it does). This of course would make it wait till they are all completed. I am looking for some code that would make it wait until one, any of them is done, and then not worry about the others.

I was informed that it would only work recursively.

  • 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-06T00:31:39+00:00Added an answer on June 6, 2026 at 12:31 am

    This doesn’t use recursion but fits the requirement to fetch from multiple datasources and only care about the first that returns a successful response.

    http://jsfiddle.net/mNJ6D/

    function raceToIt(urls) {
        var deferred = $.Deferred(),
            promises;
    
        function anyComplete(data) {
            if (!deferred.isResolved()) {
                deferred.resolveWith(this, [data]);
                promises.forEach(function(promise) {
                    promise.abort();
                });
            }
        }
        promises = urls.map(function(url) {
            return $.getJSON(url).then(anyComplete);
        });
        return deferred.promise();
    }
    raceToIt(["/echo/json/", "/echo/json/", "/echo/json/"]).then(function(data) {
        console.log(data);
    });​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This task has already been asked/answered, but I recently had a job interview that
Yesterday in my interview I was asked this question. (At that time I was
This is an interview question asked a month ago.... Do session use cookies? If
This question was asked at interview. Say I have a contract. [ServiceContract] public interface
Interview Question I have been asked this question in an interview, and the answer
This is a question that I was asked on a job interview some time
I was asked this question in an interview. If there is a pointer that
I was asked this question in a job interview recently. I answered that I
I was asked this question in a .net/C# interview: If we have two threads
One of my friend was asked this question in an interview - You have

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.