ecmascript-2017

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

How to wait for multiple asynchronous calls from for loop?

落花浮王杯 提交于 2019-12-24 11:03:37
问题 Code without any handling: for (i=0; i<dbImgCount; i++){ (function(i) { imgDownload(FolderPath[i].FolderPath, function (next){ var url = FolderPath[i].FolderPath; const img2 = cv.imread(imgDownload.Filename); match({url, img1, img2, detector: new cv.ORBDetector(), matchFunc: cv.matchBruteForceHamming, }); }) })(i); } In the above code, imgDownload is an async function which will download image, match will match features of downloaded image with another image. Need to execute a function after

AudioContext.decodeAudioData(…) not working on iPhone but working everywhere else

我与影子孤独终老i 提交于 2019-12-24 08:08:10
问题 I have the following very basic code which is part of a more complex problem. My problem here is the function: context.decodeAudioData(arrayBuffer) is not working on iPhone (tried on Safari and Chrome ) nor Mac (Safari), but it works everywhere else ( Android and Windows 10 (all browsers). Even it works on Mac (Chrome). On Mac (Safari) I get the following error: Unhandled Promise Rejection: TypeError: Not enough arguments Here is the code: window.AudioContext = window.AudioContext || window

Async/Await not working as expected with Promise.all and a .map function

烈酒焚心 提交于 2019-12-22 04:49:04
问题 I have a slew of async functions I'm using and I'm having a weird issue. My code, working, looks like: async mainAsyncFunc (metadata) { let files = metadata.map(data => this.anotherAsyncFunc(data.url)); return Promise.all(files); } anotherAsyncFunc function looks like: async anotherAsyncFunc (url) { return await axios({ url, }).then(res => res.data) .catch(err => { throw err; }); } My issue comes when I try to append more data to what the first function ( mainAsyncFunc ) returns. My thoughts