ecmascript-2017


karma-typescript: import JS file with Async keyword

不羁岁月 提交于 2020-01-14 07:48:11
问题 I'm using karma-typescript, with this karma config file : karmaTypescriptConfig: { compilerOptions: { target: "es5", lib: ["dom", "es2015", "es2017"] }, bundlerOptions: { transforms: [require("karma-typescript-es6-transform")()] } }, In my spec files, I have this code : import {} from './local/lib.js' In my lib.js, I have this code : async function() {} When executing my tests with npm test , I have this error : ERROR [source-reader.karma-typescript] Error parsing code: Unexpected token (X:Y)

How does `for..of` loop resolve the iterator from an object?

ぃ、小莉子 提交于 2020-01-11 09:29:39
问题 For an object to implement iterable interface it must implement [Symbol.iterator] key that points to a function that returns the iterator . I'm wondering if the for..of loop internally calls this method on an object to get that iterator ? The reason I'm curious is that, for example, Map defines an interface with several iterators (entries, values, keys) and it seems that if not specified explicitly the for..of loop uses the iterator returned by map.entries() call. I've trying searching in the

Is it possible to resolve async function without return keyword

杀马特。学长 韩版系。学妹 提交于 2020-01-10 18:41:18
问题 I started to use ES7 feature async/await , which gives the best approach to deal with asynchronous tasks, and makes your code cleaner and readable. However it doesn't give you an access to Promise, created by async function, so if you do some async request in your async function you should promisify it, then await it and then return the result. I mean this: async function doStuff() { //stuff... var value = await new Promise(function(resolve) { $.get('http://some/url/...', function(result) { /

How To Synchronise Promise Objects?

[亡魂溺海] 提交于 2020-01-10 05:07:09
问题 I have promise objects which need to work synchronize. For example second promise shouldn't work before first one is done. If first one rejects first one has to be executed again. I have implemented some examples.This one works well. call getVal, wait 2000ms, return, i++, again call getVal ..... getVal() { return new Promise(function(resolve, reject) { setTimeout(function(){ resolve(19) }, 2000); }); } async promiseController(){ for(var i =0;i<5;i++) { var _val = await this.getVal() console

Angular 8 Native Typescript crash-free, accessor shorthand

时光怂恿深爱的人放手 提交于 2020-01-04 09:27:47
问题 Angular 8 : I used to use as a short hack on typescript side : object['accessor']['accessor']['accessor'] to get object.accessor.accessor.accessor without running the risk of throwing an error if one of the children was empty. What is the best way to do this by today's ECMA Script standards inside the typescript file? EDIT : I found this https://medium.com/inside-rimeto/optional-chaining-in-typescript-622c3121f99b the b/ Nested Ternary Expressions seems the best but it seems convoluted EDIT 2

async/await clarity, with sleep example

╄→尐↘猪︶ㄣ 提交于 2020-01-04 01:44:16
问题 I am trying to get hang of async/await with below implementation but it is not working as expected public static async sleep(ms: number): Promise<void> { await Utilities._sleep(ms); } private static _sleep(ms: number): Promise<{}> { return new Promise((resolve: Function) => setTimeout(resolve, ms)); } _sleep will resolve promise after n milliseconds, and await should sleep till that time.. but below test of mine is failing it("should sleep for 500 ms", ()=> { const date1 = (new Date())

Selenium with async/await in JS, find and click on element

谁说胖子不能爱 提交于 2020-01-03 02:08:07
问题 I'm trying to refactor my tests using Selenium webdriver and Mocha to ES7 with async/await functionality. I have got following piece of code: await loginPage.loginAsAdmin() /* THIS DOES NOT WORK */ //await layout.Elements.routePageButton().click() /* THIS DOES WORK */ let a = await layout.Elements.routePageButton() await a.click() I don't understand why the particular does not work - I get: TypeError: layout.Elements.routePageButton(...).click is not a function Function before click method

Angular 1.5 && Async/Await && jasmine tests

心已入冬 提交于 2020-01-02 07:58:52
问题 I already looked everywhere but could not find a solution yet for my particular case. We are using angular 1.5 and a Karma/Jasmine setup for unit tests. In the initial source code, I used ES2017 async/await in the controller. That seemed to work fine as long as I added $apply of $digest manually at the end. So for example: async function loadData() { try { vm.isLoading = true; vm.data = await DataService.getData(); $scope.$apply(); } catch (ex) { vm.isLoading = false; } } To write an

ES2017 - Async vs. Yield

我的梦境 提交于 2019-12-28 12:13:31
问题 I am confused about the current discussion of adding async functions and the keyword await to the next EcmaScript. I do not understand why it is necessary to have the async keyword before the function keyword. From my point of view the await keyword to wait for a result of a generator or promise done , a function's return should be enough. await should simple be usable within normal functions and generator functions with no additional async marker. And if I need to create a function what

Loading files synchronously in Javascript with await operator

风流意气都作罢 提交于 2019-12-25 17:44:31
问题 Recently, I've read that there is a await operator in Javascript for waiting for a Promise object returned by an async function. My goal is to use just functions that are provided by the standard Javascript without the need of any external libraries. So my question is: how can I efficiently use the await operator for fetching data from server sequentially (one file after the other)? 回答1: I've managed to get what I was looking for. The first step is to create a async function and then, put all

工具导航Map