ECMAScript 6 (ES6) importing problems


#1

I want to write my code fully in es6; however, I get some errors especially in importing models. For example:

import { Observable } from 'data/observable';

it will give me the following error

SyntaxError: Importing binding name ‘Observable’ is not found.

are there any flags or configurations that I need to do to fully utilize es6?


Error: trying to link invalid ‘this’ to a java object (async/await, importHelpers)
Error: trying to link invalid ‘this’ to a java object (async/await, importHelpers)
#2

@ganas currently import statements are not supported inside the Android runtime. Extending native (Android) classes using ES6 syntax isn’t supported either. Other than that you should not be facing issues utilizing the other ES6 features to the fullest.

We’ll announce publicly when support for either is delivered in an official NativeScript release.


Unexpected token import
#3

Thanks for clarifying. Yeah I got so confused cuz some ES6 syntax works while others don’t.


#4

@ganas
I am still using require statements to import things in NS Core with JS.
I don’t mind using require statements,
but I’m happy that I can write async functions now in NS.

believe it or not, by writing async function, just yesterday I reduced a function which used
promises, from 85 lines to just under 25 lines!

async functions really let you write cleaner code and avoid the promise chain hell.


#5

Nice, what changes you made to enable ES7 to have Await/Async


#6

In my .eslintrc file, I modified the parserOptions to this, to enable async functions (actually it works without that as well, but eslint will show squiggly line under your async keyword)

"parserOptions": {
        "ecmaVersion": 2017,
        "ecmaFeatures": {
            "impliedStrict": true
        }
    }

@ganas

in the parserOptions, if you add sourceType: "module" it will let you write import statements,
but NS Core doesn’t recognise import keyword


#7

Thanks bud, I will try it :slight_smile:


#8

are you using NS Core with JS too?


#9

Yes, its the way to go awesome vanilla javascript with awesome NativeScript :wink:


#10

yay hi5!

Plain Awesome NativeScript <3


#11

So I just gave it a go, and it worked like a charm

async function AreYouTalkingToMe() {
    console.log("I'm a Async");
}

async function WhoAreYou (args) {
    await AreYouTalkingToMe();
    console.log("Yey, nice meeting you Async, long Awaited");
};

Now I have some refactoring to do


#12

Welcome to Async/Await awesomeness :slight_smile:


#13

For Android, I didn’t face any problems; however, for iOS it threw the following error

Unexpected keyword 'function'

I tried installing babel core and babel/preset-env, and added correct .babelrc file but still no luck with iOS :frowning:


#14

Strange. It didn’t throw any such error for me :thinking:


#15

May i know the path for .eslintrc file ?


#16

It is there in your project folder. If you have installed eslint.

If you have not installed eslint. Then you can directly start using async await


#17

I have not used eslint , still I am getting ReferenceError: __awaiter is not defined. Below is my code

export async function doSignInRequest() {
    console.log("Making request ");

    await makeRequest();
    console.log("Requesting finished ");

}

export async function makeRequest() {
    console.log("Requesting : from here");
}

#18

Are you using ns core?


#19

Yes. I am using NS Core .


#20

this is how my dev dependencies look

"devDependencies": {
        "babel-traverse": "6.4.5",
        "babel-types": "6.4.5",
        "babylon": "6.4.5",
        "eslint": "4.6.1",
        "lazy": "1.0.11",
        "nativescript-dev-sass": "1.3.2",
        "node-sass": "4.5.3"
    },

in package.json