I know it's a little rushed to talk about this. But if it's an excellent question! When we can see NativeScript ECMAScript 6?
We use quite latest versions of JavaScriptCore and V8. We won't lag behind the browsers in terms of language features. However most of the APIs that are DOM specific will not be introduced in {N} since we are focused at delivering native UI and prefer to keep our UI framework close to the way the native mobile frameworks work.
These dev plugins may also be helpful. They would integrate Babel or TypeScript compilers that will allow you to develop apps using new language features:
@PanayotCankov @alejonext We are actually using a slightly older version of v8 on Android. We are currently on 4.7; And the engine has had a 4.8 (Stable), 4.9 (Beta), 4.10 (Dev) and now 5.0 (Canary)
Eventually we will be on 5.0 which exposes many es6 by default. Right now you can use quite a few es6 features if you are willing to pass feature enabling flags to the v8 engine in your app package.json file. However, the reason they are behind flags is because they may not be complete or maybe buggy.
@NathanaelA @PanayotCankov If as you know is that no browser fully supports ECS 6, however I was really straining to show that if there is an interest to support it. If it is interesting to do from a code translator, however I think happen to be looking to the future!
Trying to follow this tutorial https://www.nativescript.org/blog/details/using-babel-in-nativescript-apps, but when i change target to es6 y get a lot of these in tns-core-modules:
error TS2314: Generic type 'Promise<T, R>' requires 2 type argument(s).
I am using typescript and want to do some async/await.
Empty app with typescript, change target to es2015, same errors on tns-core-modules. How can i do async/await?
@alexrainman
I think the problem is related to this one. Typescript do not transpile down to es5 on async/await. Seems like it is planed in 2.0.
http://stackoverflow.com/questions/32401741/async-await-simple-example-typescript
https://github.com/Microsoft/TypeScript/issues/1564
Using babel:
Code under worked in my test project.: (have copied it and removed some code so it is not tested)
The babel transpiler will convert the async/await to a promise. It was not what i needed. If you need to do a real "wait" you can try out the yield command. (same code in the .babelrc file).
I get error when i use yield in interfaces i override. So if you solve that please let me know. (else it seems like yield is working as expected). This is a road i would prefer to avoid if you can.
In the file .babelrc
{
"presets": ["es2015"],
"plugins": ["transform-async-to-generator"]
}
Code
require("babel-polyfill");
async function testAwait() {
//NEED TO SET THE URL.
const url = "";
const response = await fetch(url);
const result = await response.json();
//var latlngArray = result.geojson;
console.log(result.toString());
//return latlngArray;
}
@Stavanger75 how do i install this?
"plugins": ["transform-async-to-generator"]
Found it. Thanks
I wonder when async-await will be supported by default in N?
I tried this but i get this exception:
ReferenceError: __awaiter is not defined
File: "/data/data/org.nativescript.AsyncAwait/files/app/main-view-model.js, line: 41, column: 19
StackTrace:
Frame: function:'onTap', file:'/data/data/org.nativescript.AsyncAwait/files/app/main-view-model.js', line: 41, column: 20
Frame: function:'Observable.notify', file:'/data/data/org.nativescript.AsyncAwait/files/app/tns_modules/data/observable/observable.js', line: 155, column: 32
Frame: function:'Observable._emit', file:'/data/data/org.nativescript.AsyncAwait/files/app/tns_modules/data/observable/observable.js', line: 177, column: 18
Frame: function:'_android.setOnClickListener.android.view.View.OnClickListener.onClick', file:'/data/data/org.nativescript.AsyncAwait/files/app/tns_modules/ui/button/button.js', line: 33, column: 32
at com.tns.Runtime.callJSMethodNative(Native Method)
at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:862)
at com.tns.Runtime.callJSMethodImpl(Runtime.java:727)
at com.tns.Runtime.callJSMethod(Runtime.java:713)
at com.tns.Runtime.callJSMethod(Runtime.java:694)
at com.tns.Runtime.callJSMethod(Runtime.java:684)
at com.tns.gen.android.view.View_OnClickListener.onClick(View_OnClickListener.java:11)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I like the platform but it's getting really complicated to make async/await to work.
any news on this ?
@c1ngular - which part? ES6, NativeScript has pretty much full ES6 support now. Await/Async is ES7...
@NathanaelA , how about "class" support in JavaScript for instance, it is more readable and clear when you need to extend native classes in your app or plugins!
Here is how to know:
https://kangax.github.io/compat-table/es6/
The v8 Engine if I recall correctly is v8 54, and iOS is JSC as of iOS 10 I believe; so if it says it has support in the Kangax tables; odds are very likely it has support in NS. ;-)
Even if it is not supported by the runtimes, async/await are now down-compiled to es5 by TypeScript so just give it a try...
As @PanayotCankov says async/await is now compiled down to ES5 by TypeScript.
NativeScript breaks if we don't enable noEmitHelpers in tsconfig.json, so we can't out-of-the-box use async/await.
The same goes if we install tslib and enable importHelpers in tsconfig.json
BUT if we just copy the two functions ( __awaiter and __generator) that is needed from tslib and assign them to global we can use async/await in our {N}-app.
Just copy these two functions from here:
https://github.com/Microsoft/tslib/blob/90b4e189558c86cd8779d396c69a8254ce913874/tslib.js#L90-L125
Our tsconfig.json looks like this:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"declaration": false,
"removeComments": true,
"noLib": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [
"es2016"
],
"sourceMap": true,
"pretty": true,
"noEmitHelpers": true,
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"noImplicitAny": false,
"noImplicitReturns": true,
"noImplicitUseStrict": false,
"noFallthroughCasesInSwitch": true,
"noUnusedParameters": true,
"noUnusedLocals": true,
"skipLibCheck": true,
"typeRoots": [
"node_modules/@types",
"node_modules"
],
"types": [
"jasmine"
]
},
"exclude": [
"node_modules",
"platforms",
"src",
"**/*.aot.ts"
],
"compileOnSave": false
}
@m-abs, did you had any problem with runtime errors and webpacking a project with async/await helper?
@carrbrpoa
We had to make a minor change, but no problems other than that. I've made a gist with the current version: https://gist.github.com/m-abs/7f5783a51cc733717750c9d444f2f1a5
I just change noEmitHelpers to true, and target to es2016. It works now.
I've publish a helper package: https://www.npmjs.com/package/nativescript-tslib
You could as @michaeljota suggests change target, but if you need to extend native-classes that won't work.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
As @PanayotCankov says async/await is now compiled down to ES5 by TypeScript.
NativeScript breaks if we don't enable
noEmitHelpersintsconfig.json, so we can't out-of-the-box use async/await.The same goes if we install
tsliband enableimportHelpersintsconfig.jsonBUT if we just copy the two functions ( __awaiter and __generator) that is needed from
tsliband assign them toglobalwe can use async/await in our {N}-app.Just copy these two functions from here:
https://github.com/Microsoft/tslib/blob/90b4e189558c86cd8779d396c69a8254ce913874/tslib.js#L90-L125
Our
tsconfig.jsonlooks like this: