Questions tagged [ecmascript-2017]

Use this tag for questions about features finalized in ECMAScript 2017. Do *not* use this tag if the code in question merely *uses* one of the features, *unless* the feature is cause of the problem.

Proposals finalized in 2017 include:

387 questions
2999
votes
33 answers

Using async/await with a forEach loop

Are there any issues with using async/await in a forEach loop? I'm trying to loop through an array of files and await on the contents of each file. import fs from 'fs-promise' async function printFiles () { const files = await getFilePaths() //…
Saad
  • 49,729
  • 21
  • 73
  • 112
681
votes
19 answers

Combination of async function + await + setTimeout

I am trying to use the new async features and I hope solving my problem will help others in the future. This is my code which is working: async function asyncGenerator() { // other code while (goOn) { // other code var fileList…
JShinigami
  • 7,317
  • 3
  • 14
  • 22
514
votes
9 answers

Use async await with Array.map

Given the following code: var arr = [1,2,3,4,5]; var results: number[] = await arr.map(async (item): Promise => { await callAsynchronousOperation(item); return item + 1; }); which produces the following error: TS2322:…
Alon
  • 10,381
  • 23
  • 88
  • 152
468
votes
15 answers

How can I use async/await at the top level?

I have been going over async/await and after going over several articles, I decided to test things myself. However, I can't seem to wrap my head around why this does not work: async function main() { var value = await Promise.resolve('Hey…
Felipe
  • 10,606
  • 5
  • 40
  • 57
464
votes
8 answers

How to reject in async/await syntax?

How can I reject a promise that returned by an async/await function? e.g. Originally: foo(id: string): Promise { return new Promise((resolve, reject) => { someAsyncPromise().then((value)=>resolve(200)).catch((err)=>reject(400)) …
Phoenix
  • 4,773
  • 2
  • 10
  • 6
227
votes
10 answers

try/catch blocks with async/await

I'm digging into the node 7 async/await feature and keep stumbling across code like this function getQuote() { let quote = "Lorem ipsum dolor sit amet, consectetur adipiscing elit laborum."; return quote; } async function main() { try { …
Patrick
  • 7,903
  • 11
  • 52
  • 87
164
votes
6 answers

How to "await" for a callback to return?

When using a simple callback such as in the example below: test() { api.on( 'someEvent', function( response ) { return response; }); } How can the function be changed to use async / await? Specifically, assuming 'someEvent' is guaranteed…
sean2078
  • 5,131
  • 6
  • 32
  • 32
133
votes
6 answers

Correct Try...Catch Syntax Using Async/Await

I like the flatness of the new Async/Await feature available in Typescript, etc. However, I'm not sure I like the fact that I have to declare the variable I'm awaiting on the outside of a try...catch block in order to use it later. Like so: let…
freedomflyer
  • 2,431
  • 3
  • 26
  • 38
125
votes
7 answers

How to use ES8 async/await with streams?

In https://stackoverflow.com/a/18658613/779159 is an example of how to calculate the md5 of a file using the built-in crypto library and streams. var fs = require('fs'); var crypto = require('crypto'); // the file you want to get the hash var…
user779159
  • 9,034
  • 14
  • 59
  • 89
103
votes
3 answers

Property 'entries' does not exist on type 'ObjectConstructor'

I'm working on an ng2 implementation. I'm using the following function call to convert an object to an array: var authors = Object.entries(responseObject.Authors); This is a standard js function. However, the ts compiler returns the following…
user8334943
  • 1,467
  • 3
  • 11
  • 21
100
votes
3 answers

Does awaiting a non-Promise have any detectable effect?

One can await a non-Promise and that's good so. All these expressions are valid and cause no error: await 5 await 'A' await {} await null await undefined Is there any detectable effect of awaiting a non-Promise? Is there any difference in behavior…
ttulka
  • 10,309
  • 7
  • 41
  • 52
88
votes
6 answers

async/await inside arrow functions (Array#map/filter)

I'm getting compile time error in this code: const someFunction = async (myArray) => { return myArray.map(myValue => { return { id: "my_id", myValue: await service.getByValue(myValue); } }); }; Error…
WelcomeTo
  • 19,843
  • 53
  • 170
  • 286
77
votes
5 answers

how to async/await redux-thunk actions?

action.js export function getLoginStatus() { return async(dispatch) => { let token = await getOAuthToken(); let success = await verifyToken(token); if (success == true) { dispatch(loginStatus(success)); } else { …
nabeel
  • 1,181
  • 2
  • 10
  • 24
76
votes
4 answers

(ES6) class (ES2017) async / await getter

Is it or will it be possible to have an ES6 class getter return a value from an ES2017 await / async function. class Foo { async get bar() { var result = await someAsyncOperation(); return result; } } function…
Enki
  • 1,565
  • 2
  • 13
  • 20
74
votes
6 answers

async function - await not waiting for promise

I'm trying to learn async-await. In this code - const myFun = () => { let state = false; setTimeout(() => {state = true}, 2000); return new Promise((resolve, reject) => { setTimeout(() => { if(state) { …
hg_git
  • 2,884
  • 6
  • 24
  • 43
1
2 3
25 26