I really don't know why or where error !? But seems all Promises or Deferred did not work in my code, these solutions seems can not accept delays in nested calls on any structured arrays or any iterate solutions ?!
But after rebuild the code with function then await in every function, it works flawlessly. Every Ajax call will be delayed in 3s.
Here's perfect code:
const sleepNow = (delay) => new Promise((resolve) => setTimeout(resolve, delay*1000));
async function fetch_chapters(chapters) {
if(chapters.length) {
var chapter_url = chapters.shift();
await sleepNow(3);
$.ajax({
async : false,
...
success : async function(r) {
return await fetch_chapters(chapters);
}
});
}
return true;
}
async function fetch_page_story(urls) {
if( urls.length ) {
var url = urls.shift();
await sleepNow(3);
$.ajax({
async : false,
...
success : async function(r) {
r = JSON.parse(r);
if( typeof r.chapters !='undefined' )
{
await fetch_chapters(r.chapters);
}
return await fetch_page_story(urls);
}
});
}
return true;
}
await $.ajax({
async : false,
...
dataType : "json",
success : async function(msg) {
await sleepNow(3);
fetch_story(msg.urls);
}
});