Search code examples
javascriptasync-awaitmethod-chaining

Is there a short syntax in javascript for a call chain containing synchronous and asynchronous methods of the same class?


I know of 2 ways that are not very convenient and look inelegant:

  1. Via then:

    (new TestClass).syncMethod().asyncMethod().then(self => self.anotherSyncMethod())

  2. Via await:

    (await (new TestClass).syncMethod().asyncMethod()).anotherSyncMethod()

I expect it to be something like this (pseudocode):
(new TestClass).syncMethod().asyncMethod()->anotherSyncMethod()
where -> is the transition from an async method to a synchronous one.

Is there something like this in JS, and if not, is there any plans for it in the future?


Solution

  • There is not.

    However, the proposed pipeline operator might enable this in the future. You can find some bikeshedding in disussions there, like in issues #53, #86, #145, #189.