ecmascript-next

How to use async await function object in Javascript?

吃可爱长大的小学妹 提交于 2019-11-28 03:42:43
问题 Say I have a function object- setObj : function(a,b){ obj.a = a; obj.b = b; } If I have to use async & await on this function object, how do I do it? If the same was written in function (function way), say- async function setObj(a,b){ obj.a = a; obj.b = b; } await setObj(2,3); This works fine. But, how do I do it in case of function object? 回答1: If I understand your question correctly, you can just use the async keyword in front of the method declaration: let obj = {}; let myObj = { async

How do I await a list of Promises in JavaScript/TypeScript?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 02:49:26
问题 I have following code, fileStatsPromises is of Promise<Stats>[] , both foo and bar are Promise<Stats>[] . What is the correct way to await them? I want to get <Stats>[] . const files = await readDir(currentDir); const fileStatsPromises = files.map(filename => path.join(currentDir, filename)).map(stat); const foo = await fileStatsPromises; const bar = await Promise.all(fileStatsPromises); EDIT: a minimal example. function makePromise() { return Promise.resolve("hello"); } const promiseArray =

Using latest JavaScript features in TypeScript, such as ES2018

家住魔仙堡 提交于 2019-11-27 20:15:16
I have tried searching through TypeScripts documentation on their configurtion and can't seem to find the answer to what should be a simple question. Simply, how does one configure the typescript compiler so that it knows what JavaScript feature sets we are using? So for example, ES2019 lands and i think 'Ohh want to get me some of that'. In that situation what do i need to upgrade, to allow the compiler to transpile and pollyfill what it needs to? The lib option in the tsconfig confuses me and the docs don't explain much about the available libraries. I can't find anything on them directly

Does async/await will allow us to be used on constructors?

谁都会走 提交于 2019-11-27 15:53:22
问题 As the question stated. Will I be allowed to do this: class MyClass { async constructor(){ return new Promise() } } 回答1: Without trying to fortune-tell about future decisions, let's concentrate on practicality and what is already known. ES7, like ES6 before it will try to be a backwards compatible expansion to the language. With that in mind, a backwards compatible constructor function is essentially a regular function (with some runtime restrictions) that's meant to be invoked with the new

How to filter Object using Array.prototype.filter?

霸气de小男生 提交于 2019-11-27 15:39:19
Given var arr = [1,2,true,4,{"abc":123},6,7,{"def":456},9,[10]] we can filter number items within array arr using Number constructor var res = arr.filter(Number); // [1, 2, true, 4, 6, 7, 9, Array[1]] are true and [10] expected in resulting array ? If we substitute false for true at arr var arr = [1,2,false,4,{"abc":123},6,7,{"def":456},9,[10]] var res = arr.filter(Number) // [1, 2, 4, 6, 7, 9, Array[1]] using Array.isArray var res = arr.filter(Array.isArray) // [Array[1]] String var res = arr.filter(String) // [1, 2, true, 4, Object, 6, 7, Object, 9, Array[1]] If we want to filter items

Arrow vs classic method in ES6 class

元气小坏坏 提交于 2019-11-27 15:15:35
Is there any reason to write classic syntax of ES6 methods? class MyClass { myMethod() { this.myVariable++; } } When I use myMethod() as callback on some event, I must write something like this (in JSX): // Anonymous function. onClick={() => { this.myMethod(); }} // Or bind this. onClick={this.myMethod.bind(this)} But if I declare method as arrow function: class MyClass { myMethod = () => { this.myVariable++; } } than I can write just (in JSX): onClick={this.myMethod} The feature you are using is not part of ES6. It's the class fields proposal . It allows you to initialize instance properties

How to know if a function is async?

天涯浪子 提交于 2019-11-27 12:01:02
I have to pass a function to another function, and execute it as a callback. The problem is that sometimes this function is async, like: async function() { // Some async actions } So I want to execute await callback() or callback() depending on the type of function that it is receiving. Is there a way to know the type of the function?? Native async functions may be identifiable when being converted to strings : asyncFn[Symbol.toStringTag] === 'AsyncFunction' Or by AsyncFunction constructor: const AsyncFunction = (async () => {}).constructor; asyncFn instanceof AsyncFunction === true This won't

Error using async/await in React Native

谁说胖子不能爱 提交于 2019-11-27 10:43:30
问题 When trying to use async/await in react-native, I am getting the following error: uncaught error Error: SyntaxError: /Users/senthilsivanath/Documents/MusicTulip/index.ios.js: Unexpected token (50:23) 48 | renderScene: function(route,nav) { 49 | try { 50 | const response = await signIn.isLoggedIn(); My .babelrc file is: { "presets": ["react-native", "es2015", "babel-preset-stage-3"] } 回答1: You might just be missing the async keyword on line 48. Update your code to use the async keyword before

Correct use of arrow functions in React

我怕爱的太早我们不能终老 提交于 2019-11-27 06:58:44
I am using ReactJS with Babel and Webpack and using ES6 as well as the proposed class fields for arrow functions. I understand that arrow functions make things more efficient by not recreating the functions each render similar to how binding in the constructor works. However, I am not 100% sure if I am using them correctly. The following is a simplified section of my code in three different files. My code: Main.js prevItem = () => { console.log("Div is clicked") } render(){ return ( <SecondClass prevItem={this.prevItem} /> ) } SecondClass.js <ThirdClass type="prev" onClick={()=>this.props

In JavaScript, does using `await` inside a loop block the loop?

江枫思渺然 提交于 2019-11-27 06:40:12
Take the following loop: for(var i=0; i<100; ++i){ let result = await some_slow_async_function(); do_something_with_result(); } Does await block the loop? Or does the i continue to be incremented while await ing? Is the order of do_something_with_result() guaranteed sequential with regard to i ? Or does it depend on how fast the await ed function is for each i ? Does await block the loop? Or does the i continue to be incremented while await ing? "Block" is not the right word, but yes, i does not continue to be incremented while awaiting. Instead the execution jumps back to where the async