JavaScript How to use function chaining with async function? function chaining with return,function chaining in javascript,function chaining

await does not only work on Promises, but on every object that provides a .then handler ... therefore your Walker could implement a .then method to allow awaiting:

 class Walker {
   constructor(t) {
     this.t = t;
     // set up a task queue for chaining
     this.task = Promise.resolve();
    }

    // shedules a callback into the task queue
    doTask(cb) {
       // TODO: error handling
       return this.task = this.task.then(cb);
    }

    // allows to use this like a promise, e.g. "await walker";
    then(cb) { cb(this.task); }

    goToMainPage () {
      this.doTask(async () => { // shedule to chain
        await t.goTo('url/main')
      });
      return this; // return walker for chaining
   }

 }

That allows you to do:

 await walker.goToMainPage();
 await walker.goToMainPage().goToMainPage();

If you return something from inside doTask, awaiting it will resolve to that:

 returnStuff() {
   this.doTask(() => "stuff");
   return this;
 }

 //...
 console.log(await walker.returnStuff()); // "stuff"
 console.log(await walker.returnStuff().goToMainPage()); // undefined, result gets lost

Have fun with it!

Answer:1

Is Array.from(arguments) worse in some way then Array.prototype.slice.call(arguments) for creating an array of arguments? I haven't seen the former used anywhere, and the latter seems to be the ...

Is Array.from(arguments) worse in some way then Array.prototype.slice.call(arguments) for creating an array of arguments? I haven't seen the former used anywhere, and the latter seems to be the ...

I would like to perform a logical OR operation on arrays in javascript, so that performing this operation on arrays a and b let a = [0,1,0] let b = [0,0,5] gives me OR(a, b) [0,1,5] What would be ...

I would like to perform a logical OR operation on arrays in javascript, so that performing this operation on arrays a and b let a = [0,1,0] let b = [0,0,5] gives me OR(a, b) [0,1,5] What would be ...

I was wondering how I can make my canvas resize along with my browser window? As it is right now, using innerWidth and innerHeight, it will only size to the initial browser width and height. How do I ...

I was wondering how I can make my canvas resize along with my browser window? As it is right now, using innerWidth and innerHeight, it will only size to the initial browser width and height. How do I ...

  1. resize html canvas
  2. resize html canvas element
  3. html resize canvas to fit image
  4. resize canvas html javascript
  5. html resize canvas image
  6. html autosize canvas
  7. resize video canvas html
  8. html canvas resize event
  9. html canvas resize with window
  10. html canvas resize scale
  11. html canvas resize rectangle
  12. html canvas resize to fit
  13. html canvas resize shape

Having ajax request like: $.ajax({ type: 'POST', url: 'mysite/action', dataType: 'json', data: postData, success: function(response) { console.log(response); var input = $('<a ...

Having ajax request like: $.ajax({ type: 'POST', url: 'mysite/action', dataType: 'json', data: postData, success: function(response) { console.log(response); var input = $('<a ...

  1. create button jquery dynamically
  2. create button jquery
  3. adding button jquery
  4. create button using jquery
  5. create new button jquery
  6. create radio button jquery
  7. create button element jquery
  8. create toggle button jquery
  9. create html button jquery
  10. create submit button jquery
  11. create input button jquery
  12. create delete button jquery
  13. create button click event jquery
  14. create button with onclick jquery
  15. create button dynamically using jquery
  16. create dynamic radio button jquery
  17. create new radio button jquery
  18. create button on click jquery
  19. jquery create button with class
  20. jquery create button dynamically with click event