setTimeOut in an async task

14 views
Skip to first unread message

Jeff

unread,
Apr 25, 2020, 10:22:38 PM4/25/20
to Zakas Books
Hi Zakas,

I am having a question/issue on the first block code on page 162, cuz I tried fetch data with a setTimeOut, but it would cause an infinite loop and crushed when stack is over. flowed,
as follows is my code:

function fetchData() {
 return function (callback) {
   setTimeout(() => {
     console.log('cb');
     callback(null, 'fetchData');
   }, 3000);
 };
}

function secondAsyncTask() {
 return function (callback) {
   callback(null, 'secondAsyncTask');
 };
}

function thirdAsyncTask() {
 return function (callback) {
   callback(null, 'thirdAsyncTask');
 };
}

function* taskDef() {
 let result = yield fetchData();
 result = yield secondAsyncTask();
 result = yield thirdAsyncTask();
}

function run(taskDef) {
 let task = taskDef();
 let result = task.next();

  function step() {
   if (!result.done) {
     if (typeof result.value === 'function') {
       function callback(error, data) {
         if (error) {
           result = task.throw(error);
           return;
         }
         result = task.next(data);
       }
       result.value(callback);
     } else {
       result = task.next(result.value);
     }
     step();
   }
 }

  step();
}

run(taskDef);

 and I think I somehow know why it's happening.  The task next or task throw will be called 3000ms later which means that it will never reach the end, 

Jeff

unread,
Apr 25, 2020, 10:25:35 PM4/25/20
to Zakas Books
So my question is how to really simulate some time delay, otherwise this. example will not work with async tasks, TIA.

Nicholas Zakas

unread,
Apr 29, 2020, 11:27:23 AM4/29/20
to zakas...@googlegroups.com
Yes, you won’t be able to use this example with a timeout without some modification.

I’d recommend instead that you use async functions (not covered in the book). There’s really no need to use generators for this purpose now that async functions exist. 

On Sat, Apr 25, 2020 at 7:25 PM Jeff <zhenzh...@gmail.com> wrote:
So my question is how to really simulate some time delay, otherwise this. example will not work with async tasks, TIA.

--
You received this message because you are subscribed to the Google Groups "Zakas Books" group.
To unsubscribe from this group and stop receiving emails from it, send an email to zakasbooks+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/zakasbooks/3021e436-0b87-41a2-953c-0a9fd40ea30f%40googlegroups.com.

Jeff

unread,
Apr 29, 2020, 11:09:40 PM4/29/20
to Zakas Books
Thanks, 


On Wednesday, April 29, 2020 at 11:27:23 PM UTC+8, Nicholas C. Zakas wrote:
Yes, you won’t be able to use this example with a timeout without some modification.

I’d recommend instead that you use async functions (not covered in the book). There’s really no need to use generators for this purpose now that async functions exist. 
On Sat, Apr 25, 2020 at 7:25 PM Jeff <zhenzh...@gmail.com> wrote:
So my question is how to really simulate some time delay, otherwise this. example will not work with async tasks, TIA.

--
You received this message because you are subscribed to the Google Groups "Zakas Books" group.
To unsubscribe from this group and stop receiving emails from it, send an email to zakas...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages