How correctly use asynchronous with delegates?

Can anyone please give me time explaining why
else
result=task.next(result.value);
console.log(result);
Not outputs 3

function run(taskDef) {
      // create the iterator, make available elsewhere
      let task = taskDef();
  
      // start the task
      let result = task.next();
      console.log(result);
      // recursive function to keep calling next()
      function step() {
  
          // if there's more to do
          if (!result.done) {
                console.log(typeof result.value);
              if (typeof result.value === "function") {
                  result.value(function(err, data) {
                      if (err) {
                          result = task.throw(err);
                          return;
                      }
                      console.log(data);
                      result = task.next(data);
                      console.log(result);
                      step();
                  });
  
              } else {
                  result = task.next(result.value);
                  console.log(result);
                  step();
              }
          }
      }
      // start the process
      step();
  }
  run(function*() {
      let value = yield ()=>{console.log("I am a function")};             
      yield 3;
          
  });

I -believe- you’ll find the answer is ‘trace the value of task vs taskDef’.
Your console, if i were to guess, is outputting…

I am a function
undefined
Uncaught TypeError: cannot read property 'done' of undefined

My console is outputting :

{ value: [Function], done: false }
function
I am a function

No Error

Maybe this will make it more obvious…

function run(taskDef) {
      // create the iterator, make available elsewhere
      let task = taskDef();
      console.log(1);
      // start the task
      let result = task.next();
      console.log(result);
      // recursive function to keep calling next()
      function step() {
        console.log(3);
          // if there's more to do
          if (!result.done) {
		        console.log(4);
                console.log(typeof result.value);
              if (typeof result.value === "function") {
			        console.log(5);
					console.log(result)
                  result.value(function(err, data) {
				        console.log(6);
                      if (err) {
					        console.log(7);
                          result = task.throw(err);
                          return;
                      }
                      console.log(data);
                      result = task.next(data);
                      console.log(result);
					   console.log(8);
                      step();
                  });
  
              } else {
			     console.log(9)
                  result = task.next(result.value);
                  console.log(result);
                  step();
              }
          }
      }
	  console.log(2)
      // start the process
      step();
  }
  run(function*() {
      let value = yield ()=>{console.log("I am a function")};             
      yield 3;
          
  });

Hint: What’s the difference between that code and this one?

function run(taskDef) {
      // create the iterator, make available elsewhere
      let task = taskDef();
      console.log(1);
      // start the task
      let result = task.next();
      console.log(result);
      // recursive function to keep calling next()
      function step() {
        console.log(3);
          // if there's more to do
          if (!result.done) {
		        console.log(4);
                console.log(typeof result.value);
              if (typeof result.value === "function") {
			        console.log(5);
					console.log(result)
                  result.value("Boo");
  
              } else {
			     console.log(9)
                  result = task.next(result.value);
                  console.log(result);
                  step();
              }
          }
      }
	  console.log(2)
      // start the process
      step();
  }
  run(function*() {
      let value = yield ()=>{console.log("I am a function")};             
      yield 3;
          
  });

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.