ecmascript-2017

try/catch blocks with async/await

非 Y 不嫁゛ 提交于 2019-11-26 03:08:06
问题 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, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat

Combination of async function + await + setTimeout

邮差的信 提交于 2019-11-26 00:21:54
问题 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 = await listFiles(nextPageToken); var parents = await requestParents(fileList); // other code } // other code } function listFiles(token) { return gapi.client.drive.files.list({ \'maxResults\': sizeResults, \'pageToken\': token, \'q\': query }); } The problem is, that

Correct Try…Catch Syntax Using Async/Await

有些话、适合烂在心里 提交于 2019-11-25 23:16:53
问题 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 await ing on the outside of a try...catch block in order to use it later. Like so: let createdUser try { createdUser = await this.User.create(userInfo) } catch (error) { console.error(error) } console.log(createdUser) // business // logic // goes // here Please correct me if I\'m wrong, but it seems to be best practice not to place

Using async/await with a forEach loop

偶尔善良 提交于 2019-11-25 22:51:04
问题 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() // Assume this works fine files.forEach(async (file) => { const contents = await fs.readFile(file, \'utf8\') console.log(contents) }) } printFiles() This code does work, but could something go wrong with this? I had someone tell me that you\'re not