{"id":1241,"date":"2016-12-16T13:37:47","date_gmt":"2016-12-16T13:37:47","guid":{"rendered":"http:\/\/myprojects.advchaweb.com\/?p=1241"},"modified":"2024-05-22T13:45:41","modified_gmt":"2024-05-22T13:45:41","slug":"jquery-promise-cascase-to-array-reduce","status":"publish","type":"post","link":"https:\/\/myprojects.advchaweb.com\/index.php\/2016\/12\/16\/jquery-promise-cascase-to-array-reduce\/","title":{"rendered":"JQuery Promise Cascase To Array Reduce"},"content":{"rendered":"<p>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<\/p>\n<pre class=\"lang:default decode:true \">&lt;script type=\"text\/javascript\"&gt;\r\n    var Makes=[1,2,3,5,8];\r\n    $(document).ready(function(){\r\n        func1();\r\n    });\r\n    \r\n    function func1(){\r\n        timeout().then(function(){\r\n           \/\/document.body.innerHTML = \"First\";\r\n           console.log(\"1\");\r\n           return timeout();\r\n        }).then(function(){\r\n           \/\/document.body.innerHTML += \"&lt;br \/&gt;Second\";\r\n           console.log(\"2\");\r\n           return timeout();\r\n        }).then(function(){\r\n           \/\/document.body.innerHTML += \"&lt;br \/&gt;Third\";\r\n           console.log(\"3\");\r\n           return timeout();\r\n        }).then(function(){\r\n           \/\/document.body.innerHTML += \"&lt;br \/&gt;Fourth\";\r\n           console.log(\"4\");\r\n           return timeout();\r\n        }).then(function(){\r\n           \/\/document.body.innerHTML += \"&lt;br \/&gt; Last\";\r\n           console.log(\"5\");\r\n           return timeout();\r\n        });\r\n    }\r\n\r\n    function timeout(){\r\n        var d = $.Deferred();\r\n        setTimeout(function(){ \r\n            console.log(\"wait for 2 sec!\");\r\n            d.resolve(); \r\n        },2000);\r\n        return d.promise();\r\n    }\r\n    \r\n    \r\n&lt;\/script&gt;\r\n<\/pre>\n<p>And here is the output<\/p>\n<pre class=\"lang:default decode:true \">wait for 2 sec!\r\n1\r\nwait for 2 sec!\r\n2\r\nwait for 2 sec!\r\n3\r\nwait for 2 sec!\r\n4\r\nwait for 2 sec!\r\n5<\/pre>\n<p>The above code actually I borrowed from <a href=\"http:\/\/jsfiddle.net\/1njL4w1t\/\" target=\"_blank\" rel=\"noopener\">http:\/\/jsfiddle.net\/1njL4w1t\/<\/a>. NOTE: Learn carefully also about the promise error handling <a href=\"http:\/\/stackoverflow.com\/questions\/23744612\/problems-inherent-to-jquery-deferred-jquery-1-x-2-x\/23744774#23744774\" target=\"_blank\" rel=\"noopener\">Problems inherent to jQuery $.Deferred (jQuery 1.x\/2.x)<\/a>.<br \/>\nNow I want to replicate the same behavior but I want to use <a href=\"https:\/\/developer.mozilla.org\/id\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/Reduce\" target=\"_blank\" rel=\"noopener\">array reduce<\/a> here. I have tried many things to achieve that by searching through internet but can&#8217;t wrap them up in my brain. Then I asked the problem on <a href=\"http:\/\/stackoverflow.com\/questions\/41180121\/jquery-convert-promise-cascade\" target=\"_blank\" rel=\"noopener\">stackoverflow<\/a>. Here is my initial code<\/p>\n<pre class=\"lang:default decode:true \">var Makes=[1,2,3,5,8];\r\n$(document).ready(function(){\r\n    \/\/func1(); \/\/WORKS\r\n    func2();   \/\/NOT WORKS LIKE func1\r\n\r\n});\r\n\r\nfunction func1(){\r\n    timeout().then(function(){\r\n       console.log(\"1\");\r\n       return timeout();\r\n    }).then(function(){\r\n       console.log(\"2\");\r\n       return timeout();\r\n    }).then(function(){\r\n       console.log(\"3\");\r\n       return timeout();\r\n    }).then(function(){\r\n       console.log(\"4\");\r\n       return timeout();\r\n    }).then(function(){\r\n       console.log(\"5\");\r\n       return timeout();\r\n    });\r\n}\r\n\r\nfunction func2(){\r\n    \/*Makes.reduce(function(Models,Idx){\r\n        return timeout().then(function(){\r\n            console.log(Idx);\r\n            return timeout();\r\n            \/\/return $.when(timeout());\r\n        });\r\n    },0);*\/\r\n    Makes.reduce(function(Models,Idx){\r\n        return Models.then(function(){\r\n            return timeout().then(function(){\r\n                console.log(Idx);\r\n                return timeout();\r\n                \/\/return $.when(timeout());\r\n            });\r\n        });\r\n    },0);\r\n}\r\n\r\nfunction timeout(){\r\n    var d = $.Deferred();\r\n    setTimeout(function(){ \r\n        console.log(\"wait for 2 sec!\");\r\n        d.resolve(); \r\n    },2000);\r\n    return d.promise();\r\n}<\/pre>\n<p>BUT it didn&#8217;t work like I want. Then I got the working solution from one of the <a href=\"http:\/\/stackoverflow.com\/users\/5459839\/trincot\" target=\"_blank\" rel=\"noopener\">stackoverflow user<\/a>. Here is the answers (yeah I got two working answers)<\/p>\n<pre class=\"lang:default decode:true\">function func2(){\r\n    Makes.reduce(function(Models, Idx) {\r\n        return Models.then(function () {\r\n            return timeout().then(function () {\r\n                console.log(Idx);\r\n            });\r\n        });\r\n    \/\/}, $.Deferred().resolve());\r\n    }, $.when()); \/\/better than $.Deferred().resolve()?\r\n}\r\n\r\nfunction func20(){\r\n    (function repeat(Idx) {\r\n        if (Idx &gt;= Makes.length) return;\r\n        timeout().then(function () {\r\n            console.log(Makes[Idx]);\r\n            repeat(Idx+1);\r\n        });\r\n    }(0));\r\n}<\/pre>\n<p>And it&#8217;s work like I want. I made a test file (promise-timeout.php) and try it locally http:\/\/localhost\/works\/jquery\/promise-timeout.php.<\/p>\n<p>Now I want to make it more difficult by using ajax. I made two functions (&#8216;getCarQueryMake&#8217; and &#8216;getCarQueryModel&#8217;) to call the ajaxs like this:<\/p>\n<pre class=\"lang:default decode:true \">function getCarQueryMake(){\r\n    var make=$.ajax({\r\n        method: \"POST\",\r\n        url: url,\r\n        dataType: 'json',\r\n        data:{\r\n            \"type_action\" : 'check_make' \r\n        }\r\n    }).done(function (result){\r\n        \/\/return result.Makes;\r\n        \/\/make_count=result.Makes.length;\r\n        \/\/console.log(result);\r\n    });\r\n    return make;\r\n}\r\n\r\nfunction getCarQueryModel(make_id,make_display){\r\n    return function(){\r\n        var model=$.ajax({\r\n            method: \"POST\",\r\n            url: url,\r\n            dataType: 'json',\r\n            data:{\r\n                \"type_action\" : 'check_model', \r\n                \"make_id\" : make_id \r\n            }\r\n        }).done(function (result){\r\n            \/\/return result.Makes;\r\n            console.log(result);\r\n            model_count=result.Models.length;\r\n            console.log(\"Found \"+model_count+\" models for \"+make_display);\r\n        });\r\n        return model;\r\n    }\r\n}<\/pre>\n<p>Here is the url value<\/p>\n<pre class=\"lang:default decode:true \">var url=\"ajax-json.php\";<\/pre>\n<p>I created a new file &#8216;ajax-json.php&#8217; to call the carquery API.<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n    $callback=date('YmdHis');\r\n    switch($_POST[\"type_action\"]){\r\n        case 'check_make':\r\n            $data = '{\"Makes\":[{\"make_id\":\"ac\",\"make_display\":\"AC\",\"make_is_common\":\"0\",\"make_country\":\"UK\"},{\"make_id\":\"allard\",\"make_display\":\"Allard\",\"make_is_common\":\"0\",\"make_country\":\"UK\"}]}';\r\n            $results=json_decode($data, true);\r\n            break;\r\n        case 'check_model':\r\n            \/*$data = '{\"Makes\":[{\"make_id\":\"ac\",\"make_display\":\"AC\",\"make_is_common\":\"0\",\"make_country\":\"UK\"},{\"make_id\":\"allard\",\"make_display\":\"Allard\",\"make_is_common\":\"0\",\"make_country\":\"UK\"}]}';\r\n            $results=json_decode($data, true);*\/\r\n            \r\n            $makeId=$_POST[\"make_id\"];\r\n            $url='http:\/\/www.carqueryapi.com\/api\/0.3\/?callback='.$callback.'&amp;cmd=getModels&amp;make='.str_replace(\"\\'\",\"%27\",$makeId);\r\n            \r\n            $ch=curl_init();\r\n            curl_setopt($ch,CURLOPT_URL,$url);\r\n            curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);\r\n            curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);\r\n            $curl_data=curl_exec($ch);\r\n            if(!$curl_data){\r\n                $err=curl_error($ch);\r\n                $err_no=curl_errno($ch);\r\n                $err_desc='{\"error\":\"'.$err.';Error No:'.$err_no.'\"}';\r\n                return json_decode($err_desc, true);\r\n            }\r\n            $curl_data = str_replace($callback.\"(\",\"\",$curl_data);\r\n            $curl_data = str_replace(\");\",\"\",$curl_data);\r\n            $json_data = json_decode($curl_data, true);\r\n            $results=$json_data;\r\n                \r\n            break;\r\n    }\r\n    echo json_encode($results);\r\n?&gt;<\/pre>\n<p>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:<\/p>\n<pre class=\"lang:default decode:true\">&lt;script type=\"text\/javascript\"&gt;\r\n    var url=\"ajax-json.php\";\r\n    \r\n    $(document).ready(function(){\r\n        $.when(getCarQueryMake()).then(function(make){\r\n            var makes=make.Makes;\r\n            makes.reduce(function(model,makeIdx){\r\n                return model.then(function(){\r\n                    return timeout().then(function () {\r\n                        console.log(makeIdx.make_display);\r\n                    })\r\n                    .then(getCarQueryModel(makeIdx.make_id, makeIdx.make_display));\r\n                });\r\n            \/\/},$.Deferred().resolve());    \/\/WORKS ALSO WITH $.when()\r\n            },$.when());\r\n            \r\n        });\r\n    });\r\n\r\n    function timeout(){\r\n        var d = $.Deferred();\r\n        setTimeout(function(){ console.log(\"waiting for 5 sec!\");d.resolve(); },5000);\r\n        return d.promise();\r\n    }\r\n    \r\n    function getCarQueryMake(){\r\n        ...\r\n    }\r\n\r\n    function getCarQueryModel(make_id,make_display){\r\n        ...\r\n    }\r\n&lt;\/script&gt;\r\n<\/pre>\n<p>I made a local test file (promise-timeout2.php) and url: http:\/\/localhost\/works\/jquery\/promise-timeout2.php. Here is the console output:<\/p>\n<pre class=\"lang:default decode:true \">waiting for 5 sec!\r\nAC\r\nObject {Models: Array[8]}\r\nFound 8 models for AC\r\nwaiting for 5 sec!\r\nAllard\r\nObject {Models: Array[13]}\r\nFound 13 models for Allard<\/pre>\n<p>Here is the API url to get Car Models from Car Make: http:\/\/www.carqueryapi.com\/api\/0.3\/?callback=1481095214&amp;cmd=getModels&amp;make=abarth<br \/>\nThe API response look like:<\/p>\n<pre class=\"lang:default decode:true \">{\"Models\":[{\"model_name\":\"1000\",\"model_make_id\":\"abarth\"},{\"model_name\":\"1000 Bialbero \",\"model_make_id\":\"abarth\"},{\"model_name\":\"1000 GT\",\"model_make_id\":\"abarth\"},{\"model_name\":\"1000 TC Corsa\",\"model_make_id\":\"abarth\"},{\"model_name\":\"103 GT\",\"model_make_id\":\"abarth\"},{\"model_name\":\"124\",\"model_make_id\":\"abarth\"},{\"model_name\":\"1300\",\"model_make_id\":\"abarth\"},{\"model_name\":\"1500\",\"model_make_id\":\"abarth\"},{\"model_name\":\"1600\",\"model_make_id\":\"abarth\"},{\"model_name\":\"2000\",\"model_make_id\":\"abarth\"},{\"model_name\":\"205\",\"model_make_id\":\"abarth\"},{\"model_name\":\"207\",\"model_make_id\":\"abarth\"},{\"model_name\":\"208\",\"model_make_id\":\"abarth\"},{\"model_name\":\"209\",\"model_make_id\":\"abarth\"},{\"model_name\":\"210\",\"model_make_id\":\"abarth\"},{\"model_name\":\"2200\",\"model_make_id\":\"abarth\"},{\"model_name\":\"2400\",\"model_make_id\":\"abarth\"},{\"model_name\":\"500\",\"model_make_id\":\"abarth\"},{\"model_name\":\"595\",\"model_make_id\":\"abarth\"},{\"model_name\":\"600\",\"model_make_id\":\"abarth\"},{\"model_name\":\"700\",\"model_make_id\":\"abarth\"},{\"model_name\":\"750\",\"model_make_id\":\"abarth\"},{\"model_name\":\"800 Scorpione Coupe Allemano\",\"model_make_id\":\"abarth\"},{\"model_name\":\"850\",\"model_make_id\":\"abarth\"},{\"model_name\":\"A 112\",\"model_make_id\":\"abarth\"},{\"model_name\":\"Bialbero\",\"model_make_id\":\"abarth\"},{\"model_name\":\"Coupe\",\"model_make_id\":\"abarth\"},{\"model_name\":\"Grande Punto\",\"model_make_id\":\"abarth\"},{\"model_name\":\"Lancia 037\",\"model_make_id\":\"abarth\"},{\"model_name\":\"Mono 1000\",\"model_make_id\":\"abarth\"},{\"model_name\":\"Monomille\",\"model_make_id\":\"abarth\"},{\"model_name\":\"Monotipo\",\"model_make_id\":\"abarth\"},{\"model_name\":\"OT\",\"model_make_id\":\"abarth\"},{\"model_name\":\"Renault\",\"model_make_id\":\"abarth\"},{\"model_name\":\"Simca\",\"model_make_id\":\"abarth\"},{\"model_name\":\"Spider Riviera\",\"model_make_id\":\"abarth\"},{\"model_name\":\"Stola\",\"model_make_id\":\"abarth\"}]}<\/pre>\n<p>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 &#8216;getCarQuerySerie&#8217;. The function consist of some return function and the delay\/timeout. Here is the code<\/p>\n<pre class=\"lang:default decode:true \">function getCarQuerySerie(){\r\n    return function(model){\r\n        var models=model.Models;\r\n        return models.reduce(function(models,modelIdx){\r\n            return models.then(function(){\r\n                return timeout().then(function () {\r\n                    console.log(modelIdx.model_name );\r\n                })\r\n                .then(function(){\r\n                    var serie=$.ajax({\r\n                        method: \"POST\",\r\n                        url: url,\r\n                        dataType: 'json',\r\n                        data:{\r\n                            \"type_action\" : 'check_serie', \r\n                            \"make_id\" : modelIdx.model_make_id, \r\n                            \"model_id\" : modelIdx.model_name \r\n                        }\r\n                    }).done(function (result){\r\n                        \/\/return result.Makes;\r\n                        console.log(result);\r\n                        \/\/model_count=result.Series.length;\r\n                        \/\/console.log(\"Found \"+model_count+\" models for \"+make_display);\r\n                    });\r\n                    return serie;\r\n                });\r\n                \r\n            });\r\n        }, $.when());\r\n    }\r\n}<\/pre>\n<p>I need to call this function like this<\/p>\n<pre class=\"lang:default decode:true\">$(document).ready(function(){\r\n    $.when(getCarQueryMake()).then(function(make){\r\n        var makes=make.Makes;\r\n        makes.reduce(function(model,makeIdx){\r\n            return model.then(function(){\r\n                return timeout().then(function () {\r\n                    console.log(makeIdx.make_display);\r\n                })\r\n                .then(getCarQueryModel(makeIdx.make_id, makeIdx.make_display))\r\n                .then(getCarQuerySerie());\r\n            });\r\n        \/\/},$.Deferred().resolve());    \/\/WORKS ALSO WITH $.when()\r\n        },$.when());\r\n        \r\n    });\r\n});<\/pre>\n<p>Modify ajax-json.php file to handle the ajax call<\/p>\n<pre class=\"lang:default decode:true \">...\r\ncase 'check_serie':\r\n    $makeId=$_POST[\"make_id\"];\r\n    $modelId=$_POST[\"model_id\"];\r\n    $url='http:\/\/www.carqueryapi.com\/api\/0.3\/?callback='.$callback.'&amp;cmd=getTrims&amp;make='.str_replace(\"\\'\",\"%27\",$makeId).'&amp;year=-1&amp;model='.str_replace(\"\\'\",\"%27\",$modelId);\r\n    \r\n    $ch=curl_init();\r\n    curl_setopt($ch,CURLOPT_URL,$url);\r\n    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);\r\n    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);\r\n    $curl_data=curl_exec($ch);\r\n    if(!$curl_data){\r\n        $err=curl_error($ch);\r\n        $err_no=curl_errno($ch);\r\n        $err_desc='{\"error\":\"'.$err.';Error No:'.$err_no.'\"}';\r\n        return json_decode($err_desc, true);\r\n    }\r\n    $curl_data = str_replace($callback.\"(\",\"\",$curl_data);\r\n    $curl_data = str_replace(\");\",\"\",$curl_data);\r\n    $json_data = json_decode($curl_data, true);\r\n    $results=$json_data;\r\n        \r\n    break;<\/pre>\n<p>I made a test file (promise-timeout3.php) and the url:\u00a0http:\/\/localhost\/works\/jquery\/promise-timeout3.php. Here is the console output (Here I only use one Make. It&#8217;s &#8216;AC&#8217; because the others Makes has many models and series. &#8216;AC&#8217; only has 8 models):<\/p>\n<pre class=\"lang:default decode:true \">waiting for 5 sec!\r\nAC\r\nObject {Models: Array[8]}\r\nFound 8 models for AC\r\nwaiting for 5 sec!\r\n2-Litre\r\nObject {Trims: Array[7]}\r\nwaiting for 5 sec!\r\n427\r\nObject {Trims: Array[1]}\r\nwaiting for 5 sec!\r\n428\r\nObject {Trims: Array[14]}\r\nwaiting for 5 sec!\r\nAce\r\nObject {Trims: Array[15]}\r\nwaiting for 5 sec!\r\nAceca\r\nObject {Trims: Array[30]}\r\nwaiting for 5 sec!\r\nAceca-Bristol\r\nObject {Trims: Array[1]}\r\nwaiting for 5 sec!\r\nCobra\r\nObject {Trims: Array[20]}\r\nwaiting for 5 sec!\r\nGreyhound\r\nObject {Trims: Array[7]}<\/pre>\n<p>Here is the API url to get Car Series\/Trims from Car Models: http:\/\/www.carqueryapi.com\/api\/0.3\/?callback=1481095214&amp;cmd=getTrims&amp;make=abarth&amp;year=-1&amp;model=1000&amp;full_results=0<br \/>\nThen one of the Car Series\/Trims output:<\/p>\n<pre class=\"lang:default decode:true \">{\"Trims\":[{\"model_id\":\"192\",\"model_make_id\":\"ac\",\"model_name\":\"Greyhound\",\"model_trim\":\"\",\"model_year\":\"1963\",\"model_body\":null,\"model_engine_position\":\"Front\",\"model_engine_cc\":\"1971\",\"model_engine_cyl\":\"6\",\"model_engine_type\":\"in-line\",\"model_engine_valves_per_cyl\":\"2\",\"model_engine_power_ps\":\"125\",\"model_engine_power_rpm\":\"5750\",\"model_engine_torque_nm\":\"179\",\"model_engine_torque_rpm\":null,\"model_engine_bore_mm\":null,\"model_engine_stroke_mm\":null,\"model_engine_compression\":null,\"model_engine_fuel\":\"Gasoline\",\"model_top_speed_kph\":null,\"model_0_to_100_kph\":null,\"model_drive\":null,\"model_transmission_type\":null,\"model_seats\":\"4\",\"model_doors\":\"2\",\"model_weight_kg\":\"1015\",\"model_length_mm\":\"4580\",\"model_width_mm\":\"1670\",\"model_height_mm\":\"1340\",\"model_wheelbase_mm\":\"2550\",\"model_lkm_hwy\":null,\"model_lkm_mixed\":null,\"model_lkm_city\":null,\"model_fuel_cap_l\":\"54\",\"model_sold_in_us\":\"1\",\"model_co2\":null,\"model_make_display\":\"AC\",\"make_display\":\"AC\",\"make_country\":\"UK\"},{\"model_id\":\"181\",\"model_make_id\":\"ac\",\"model_name\":\"Greyhound\",\"model_trim\":\"\",\"model_year\":\"1962\",\"model_body\":null,\"model_engine_position\":\"Front\",\"model_engine_cc\":\"1971\",\"model_engine_cyl\":\"6\",\"model_engine_type\":\"in-line\",\"model_engine_valves_per_cyl\":\"2\",\"model_engine_power_ps\":\"125\",\"model_engine_power_rpm\":\"5750\",\"model_engine_torque_nm\":\"179\",\"model_engine_torque_rpm\":null,\"model_engine_bore_mm\":null,\"model_engine_stroke_mm\":null,\"model_engine_compression\":null,\"model_engine_fuel\":\"Gasoline\",\"model_top_speed_kph\":null,\"model_0_to_100_kph\":null,\"model_drive\":null,\"model_transmission_type\":null,\"model_seats\":\"4\",\"model_doors\":\"2\",\"model_weight_kg\":\"1015\",\"model_length_mm\":\"4580\",\"model_width_mm\":\"1670\",\"model_height_mm\":\"1340\",\"model_wheelbase_mm\":\"2550\",\"model_lkm_hwy\":null,\"model_lkm_mixed\":null,\"model_lkm_city\":null,\"model_fuel_cap_l\":\"54\",\"model_sold_in_us\":\"1\",\"model_co2\":null,\"model_make_display\":\"AC\",\"make_display\":\"AC\",\"make_country\":\"UK\"},{\"model_id\":\"222\",\"model_make_id\":\"ac\",\"model_name\":\"Greyhound\",\"model_trim\":\"\",\"model_year\":\"1961\",\"model_body\":null,\"model_engine_position\":\"Front\",\"model_engine_cc\":\"1971\",\"model_engine_cyl\":\"6\",\"model_engine_type\":\"in-line\",\"model_engine_valves_per_cyl\":\"2\",\"model_engine_power_ps\":\"125\",\"model_engine_power_rpm\":\"5750\",\"model_engine_torque_nm\":\"179\",\"model_engine_torque_rpm\":null,\"model_engine_bore_mm\":null,\"model_engine_stroke_mm\":null,\"model_engine_compression\":null,\"model_engine_fuel\":\"Gasoline\",\"model_top_speed_kph\":null,\"model_0_to_100_kph\":null,\"model_drive\":null,\"model_transmission_type\":null,\"model_seats\":\"4\",\"model_doors\":\"2\",\"model_weight_kg\":\"1015\",\"model_length_mm\":\"4580\",\"model_width_mm\":\"1670\",\"model_height_mm\":\"1340\",\"model_wheelbase_mm\":\"2550\",\"model_lkm_hwy\":null,\"model_lkm_mixed\":null,\"model_lkm_city\":null,\"model_fuel_cap_l\":\"54\",\"model_sold_in_us\":\"1\",\"model_co2\":null,\"model_make_display\":\"AC\",\"make_display\":\"AC\",\"make_country\":\"UK\"},{\"model_id\":\"223\",\"model_make_id\":\"ac\",\"model_name\":\"Greyhound\",\"model_trim\":\"\",\"model_year\":\"1960\",\"model_body\":null,\"model_engine_position\":\"Front\",\"model_engine_cc\":\"1971\",\"model_engine_cyl\":\"6\",\"model_engine_type\":\"in-line\",\"model_engine_valves_per_cyl\":\"2\",\"model_engine_power_ps\":\"125\",\"model_engine_power_rpm\":\"5750\",\"model_engine_torque_nm\":null,\"model_engine_torque_rpm\":null,\"model_engine_bore_mm\":\"66.0\",\"model_engine_stroke_mm\":\"96.0\",\"model_engine_compression\":null,\"model_engine_fuel\":\"Gasoline\",\"model_top_speed_kph\":null,\"model_0_to_100_kph\":null,\"model_drive\":null,\"model_transmission_type\":null,\"model_seats\":\"4\",\"model_doors\":\"2\",\"model_weight_kg\":\"1015\",\"model_length_mm\":\"4580\",\"model_width_mm\":\"1670\",\"model_height_mm\":\"1340\",\"model_wheelbase_mm\":\"2550\",\"model_lkm_hwy\":null,\"model_lkm_mixed\":null,\"model_lkm_city\":null,\"model_fuel_cap_l\":\"54\",\"model_sold_in_us\":\"1\",\"model_co2\":null,\"model_make_display\":\"AC\",\"make_display\":\"AC\",\"make_country\":\"UK\"},{\"model_id\":\"202\",\"model_make_id\":\"ac\",\"model_name\":\"Greyhound\",\"model_trim\":\"\",\"model_year\":\"1959\",\"model_body\":null,\"model_engine_position\":\"Front\",\"model_engine_cc\":\"1969\",\"model_engine_cyl\":\"6\",\"model_engine_type\":\"in-line\",\"model_engine_valves_per_cyl\":\"2\",\"model_engine_power_ps\":\"125\",\"model_engine_power_rpm\":\"5750\",\"model_engine_torque_nm\":\"179\",\"model_engine_torque_rpm\":\"4500\",\"model_engine_bore_mm\":\"66.0\",\"model_engine_stroke_mm\":\"96.0\",\"model_engine_compression\":\"9.0:1\",\"model_engine_fuel\":\"Gasoline\",\"model_top_speed_kph\":\"167\",\"model_0_to_100_kph\":null,\"model_drive\":\"Rear\",\"model_transmission_type\":\"Manual\",\"model_seats\":null,\"model_doors\":\"2\",\"model_weight_kg\":\"1015\",\"model_length_mm\":\"4580\",\"model_width_mm\":\"1660\",\"model_height_mm\":\"1340\",\"model_wheelbase_mm\":\"2550\",\"model_lkm_hwy\":null,\"model_lkm_mixed\":null,\"model_lkm_city\":null,\"model_fuel_cap_l\":\"54\",\"model_sold_in_us\":\"1\",\"model_co2\":null,\"model_make_display\":\"AC\",\"make_display\":\"AC\",\"make_country\":\"UK\"},{\"model_id\":\"195\",\"model_make_id\":\"ac\",\"model_name\":\"Greyhound\",\"model_trim\":\"2200\",\"model_year\":\"1959\",\"model_body\":null,\"model_engine_position\":\"Front\",\"model_engine_cc\":\"2216\",\"model_engine_cyl\":\"6\",\"model_engine_type\":\"in-line\",\"model_engine_valves_per_cyl\":\"2\",\"model_engine_power_ps\":\"105\",\"model_engine_power_rpm\":\"4700\",\"model_engine_torque_nm\":\"175\",\"model_engine_torque_rpm\":\"4700\",\"model_engine_bore_mm\":null,\"model_engine_stroke_mm\":null,\"model_engine_compression\":null,\"model_engine_fuel\":\"Gasoline\",\"model_top_speed_kph\":null,\"model_0_to_100_kph\":null,\"model_drive\":null,\"model_transmission_type\":null,\"model_seats\":null,\"model_doors\":\"4\",\"model_weight_kg\":\"1093\",\"model_length_mm\":\"4580\",\"model_width_mm\":\"1670\",\"model_height_mm\":\"1340\",\"model_wheelbase_mm\":\"2550\",\"model_lkm_hwy\":null,\"model_lkm_mixed\":null,\"model_lkm_city\":null,\"model_fuel_cap_l\":\"54\",\"model_sold_in_us\":\"1\",\"model_co2\":null,\"model_make_display\":\"AC\",\"make_display\":\"AC\",\"make_country\":\"UK\"},{\"model_id\":\"154\",\"model_make_id\":\"ac\",\"model_name\":\"Greyhound\",\"model_trim\":\"2600\",\"model_year\":\"1959\",\"model_body\":null,\"model_engine_position\":\"Front\",\"model_engine_cc\":\"2553\",\"model_engine_cyl\":\"6\",\"model_engine_type\":\"in-line\",\"model_engine_valves_per_cyl\":\"2\",\"model_engine_power_ps\":\"170\",\"model_engine_power_rpm\":\"5500\",\"model_engine_torque_nm\":null,\"model_engine_torque_rpm\":null,\"model_engine_bore_mm\":\"82.6\",\"model_engine_stroke_mm\":\"79.5\",\"model_engine_compression\":null,\"model_engine_fuel\":\"Gasoline\",\"model_top_speed_kph\":null,\"model_0_to_100_kph\":null,\"model_drive\":null,\"model_transmission_type\":null,\"model_seats\":null,\"model_doors\":\"4\",\"model_weight_kg\":\"1040\",\"model_length_mm\":\"4580\",\"model_width_mm\":\"1670\",\"model_height_mm\":\"1340\",\"model_wheelbase_mm\":\"2550\",\"model_lkm_hwy\":null,\"model_lkm_mixed\":null,\"model_lkm_city\":null,\"model_fuel_cap_l\":\"54\",\"model_sold_in_us\":\"1\",\"model_co2\":null,\"model_make_display\":\"AC\",\"make_display\":\"AC\",\"make_country\":\"UK\"}]}<\/pre>\n<p>NEXT. I need to update the local database also from the APIs but I think it&#8217;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???<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/myprojects.advchaweb.com\/index.php\/2016\/12\/16\/jquery-promise-cascase-to-array-reduce\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;JQuery Promise Cascase To Array Reduce&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[],"class_list":["post-1241","post","type-post","status-publish","format-standard","hentry","category-jquery"],"_links":{"self":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts\/1241","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/comments?post=1241"}],"version-history":[{"count":8,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts\/1241\/revisions"}],"predecessor-version":[{"id":11756,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts\/1241\/revisions\/11756"}],"wp:attachment":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/media?parent=1241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/categories?post=1241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/tags?post=1241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}