jQuery Deferred and Promise

Tutorial
Read: https://www.sitepoint.com/introduction-jquery-deferred-objects/
https://www.geeksforgeeks.org/what-are-deferred-and-promise-object-in-jquery/
Deferred samples
Find the file in /home/satria/Documents/jquery/deferred.html

Deferred for the recursive sample
find the sample at /home/satria/Documents/jquery/deferred-recursive.html

 

JQuery Promise Cascase To Array Reduce

I have a question about JQuery Promise problem. Actually I have a more difficult problem about the JQuery and Ajax that I need to loop a result from Ajax call then nest it to call another Ajax. But I dont know how to set a delay/timeout between the Ajax calls so I can avoid to put much burden on the server. I want to simplify the problem so I can comprehend the promise better. I have a working function to write some console outputs but the output has to be written after delaying for 2 seconds. Here is the code

And here is the output

The above code actually I borrowed from http://jsfiddle.net/1njL4w1t/. NOTE: Learn carefully also about the promise error handling Problems inherent to jQuery $.Deferred (jQuery 1.x/2.x).
Now I want to replicate the same behavior but I want to use array reduce here. I have tried many things to achieve that by searching through internet but can’t wrap them up in my brain. Then I asked the problem on stackoverflow. Here is my initial code

BUT it didn’t work like I want. Then I got the working solution from one of the stackoverflow user. Here is the answers (yeah I got two working answers)

And it’s work like I want. I made a test file (promise-timeout.php) and try it locally http://localhost/works/jquery/promise-timeout.php.

Now I want to make it more difficult by using ajax. I made two functions (‘getCarQueryMake’ and ‘getCarQueryModel’) to call the ajaxs like this:

Here is the url value

I created a new file ‘ajax-json.php’ to call the carquery API.

Finally after learning more from the previous sample how to setup the promise and the time out, I can manage how to setup the promise to ajax calls and the timeout to delay every ajax calls. Here is the code:

I made a local test file (promise-timeout2.php) and url: http://localhost/works/jquery/promise-timeout2.php. Here is the console output:

Here is the API url to get Car Models from Car Make: http://www.carqueryapi.com/api/0.3/?callback=1481095214&cmd=getModels&make=abarth
The API response look like:

Okay. Then I expand and add more nest to get Car Serie from Car Model. Also add a delay 5 second between the ajax call to get the Car Serie/Trim. I added a new function ‘getCarQuerySerie’. The function consist of some return function and the delay/timeout. Here is the code

I need to call this function like this

Modify ajax-json.php file to handle the ajax call

I made a test file (promise-timeout3.php) and the url: http://localhost/works/jquery/promise-timeout3.php. Here is the console output (Here I only use one Make. It’s ‘AC’ because the others Makes has many models and series. ‘AC’ only has 8 models):

Here is the API url to get Car Series/Trims from Car Models: http://www.carqueryapi.com/api/0.3/?callback=1481095214&cmd=getTrims&make=abarth&year=-1&model=1000&full_results=0
Then one of the Car Series/Trims output:

NEXT. I need to update the local database also from the APIs but I think it’d need more time to make the local database as complete as CarQuery database. Like I said previously, I dont want to burden the CarQuery server from the API requests! May be I need to increase the delay from 5 seconds to 10 seconds each API request??? Also need to add progress bar and how to handle/catch any error???