[TIMOB-25740] TiAPI: Add support for async/await
GitHub Issue | n/a |
---|---|
Type | Improvement |
Priority | High |
Status | Closed |
Resolution | Fixed |
Resolution Date | 2020-01-08T14:52:48.000+0000 |
Affected Version/s | n/a |
Fix Version/s | Release 8.0.0 |
Components | Android, iOS, Windows |
Labels | n/a |
Reporter | Hans Knöchel |
Assignee | Gary Mathews |
Created | 2018-02-01T09:26:23.000+0000 |
Updated | 2021-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!
async await
supported can be added by including the [ti.es6](https://github.com/appcelerator/ti.es6) pluginmaster: https://github.com/appcelerator/node-titanium-sdk/pull/32 *TEST CASE*
This should be resolved by now.
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!
[~hknoechel] we're repurposing TIMOB-24549 as an overarching epic for that. So I'm going to close this ticket out
Currently, this is unusable with liveview
[~bgarcia] could you please file a new ticket with the details of the issue