Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-25740] TiAPI: Add support for async/await

GitHub Issuen/a
TypeImprovement
PriorityHigh
StatusClosed
ResolutionFixed
Resolution Date2020-01-08T14:52:48.000+0000
Affected Version/sn/a
Fix Version/sRelease 8.0.0
ComponentsAndroid, iOS, Windows
Labelsn/a
ReporterHans Knöchel
AssigneeGary Mathews
Created2018-02-01T09:26:23.000+0000
Updated2021-04-06T09:16:13.000+0000

Description

We added support for ES6 and later in the latest 7.1.0 changes, which is *awesome*! The only thing missing is the support for using async/await in Titanium code. Here is an example (taken from my sample-project):
  someOtherMethod() {
    this._getUserLocation();
  }

  async _getUserLocation() {
    // FIXME: Current throws, work in progress!
    const coordinates = await Ti.Geolocation.getCurrentPosition(event => {
      return new Promise(resolve => {
        resolve(event.coords);
      });
    });
    alert(Found location! Latitude: ${coordinates.latitude}, Longitude: ${coordinates.longitude});
  }
The full sample can be found [here](https://github.com/hansemannn/titanium-es6-sample/blob/master/Resources/src/application.js#L84-L94). It is throwing an "Cannot find regeneratorRuntime" error when calling the method, which looks like a Babel error that occurs when certain transform-plugins are missing. We use the babel-preset-env, which should already support it by having a dependency on [babel-plugin-transform-async-to-generator](https://www.npmjs.com/package/babel-plugin-transform-async-to-generator) but for some reasons it does not work. Important to say is also that I may be doing something wrong, so let me know if thats the case!

Comments

  1. Gary Mathews 2018-03-21

    async await supported can be added by including the [ti.es6](https://github.com/appcelerator/ti.es6) plugin
  2. Gary Mathews 2018-06-12

    master: https://github.com/appcelerator/node-titanium-sdk/pull/32 *TEST CASE*
       const win = Ti.UI.createWindow({
               backgroundColor: 'gray'
           }),
           view = Ti.UI.createView({
               backgroundColor: 'red',
               width: 200,
               height: 200
           }),
           matrix = Ti.UI.create2DMatrix({
               rotate: 90
           }),
           animation = Ti.UI.createAnimation({
               transform: matrix,
               duration: 3000
           }),
           animate = (view, animation) => {
               return new Promise(resolve => view.animate(animation, resolve));
           };
       
       win.addEventListener('postlayout', async () => {
           await animate(view, animation);
           view.backgroundColor = 'orange';
           alert('DONE ANIMATION!');
       });
       
       win.add(view);
       win.open();
       
  3. Hans Knöchel 2019-10-15

    This should be resolved by now.
  4. Hans Knöchel 2020-01-08

    Do we have a ticket for the "real" async/await in native Ti methods? Replacing callbacks with methods returning a promise? I think Gary had a "ti.es6" cli hook that did that on a JS level, but this could likely be done natively as well!
  5. Ewan Harris 2020-01-28

    [~hknoechel] we're repurposing TIMOB-24549 as an overarching epic for that. So I'm going to close this ticket out
  6. Brian García 2021-03-31

    Currently, this is unusable with liveview
  7. Ewan Harris 2021-04-06

    [~bgarcia] could you please file a new ticket with the details of the issue

JSON Source