ecmascript-2017

Using async/await with a forEach loop

妖精的绣舞 提交于 2019-12-04 18:33:18
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 supposed to use async / await in a higher order function like this, so I just wanted to ask if there was any

Why does .catch() not catch reject() within Promise constructor within loop at an async function unless Error is passed?

主宰稳场 提交于 2019-12-04 12:08:44
Given (async () => { const p = await new Promise((resolve, reject) => setTimeout(() => { reject(new Error(1)) }, Math.floor(Math.random() * 1000))); return p})() .then(data => console.log(data)) .catch(err => console.error(err)); the Error() is logged at .catch() If we extend the pattern to use a loop the Error is logged at .catch() const fn = async(res, ...props) => { for (let prop of props) res.push(await prop()) return res } const arr = [ () => new Promise((resolve, reject) => setTimeout(() => reject(new Error(1)) , Math.floor(Math.random() * 1000)) ), () => new Promise((resolve, reject) =>

JavaScript await by default instead of manually

我怕爱的太早我们不能终老 提交于 2019-12-04 02:20:15
问题 Async/await are really handy, but I want the opposite of their behavior. Instead of other functions continuing on unless I manually ask them to await a promise, I want functions to yield unless I manually specify that they continue running in parallel. For instance, this code would print out 1 3 2 : function wait(ms) { return new Promise(r => setTimeout(r, ms)); } async function a() { console.log("1"); await wait(5000); console.log("2"); } a(); console.log("3"); I want it to print out 1 2 3 ,

How do i wrap a callback with async await?

守給你的承諾、 提交于 2019-12-04 01:24:12
My function resolves a promise that resolves as soon as the http server starts. This is my code: function start() { return new Promise((resolve, reject) { this.server = Http.createServer(app); this.server.listen(port, () => { resolve(); }); }) } How do i convert the start function to async/await? Include async before the function declaration and await the Promise constructor. Though note, you would essentially be adding code to the existing pattern. await converts a value to a Promise , though the code at Question already uses Promise constructor. async function start() { let promise = await

What's the actual use of the Atomics object in ECMAScript?

做~自己de王妃 提交于 2019-12-03 16:34:38
问题 The ECMAScript specification defines the Atomics object in the section 24.4 . Among all the global objects this is the more obscure for me since I didn't know about its existence until I didn't read its specification, and also Google hasn't many references to it (or maybe the name is too much generic and everything gets submerged?). According its official definition The Atomics object provides functions that operate indivisibly (atomically) on shared memory array cells as well as functions

Returning an awaited value returns a Promise? (es7 async/await)

蓝咒 提交于 2019-12-03 16:29:41
问题 const ret = () => new Promise(resolve => setTimeout( () => resolve('somestring'), 1000)); async function wrapper() { let someString = await ret(); return someString; } console.log( wrapper() ); It logs Promise { <pending> } ; Why does it return a Promise instead of 'somestring' ? I'm using the Babel ES7 preset to compile this. 回答1: Async functions return promises. In order to do what you want, try something like this wrapper().then(someString => console.log(someString)); You can also await on

Why `async/await` doesn't work in my case?

十年热恋 提交于 2019-12-03 16:27:25
I read about async/await , but I've a critical question. At first I explain an old example to show base of my question and then I ask my exact question. Everybody know it: console.log('1'); console.log('2'); console.log('3'); // Ex: 123 It is simple but in below case: console.log('1'); setTimeout(()=>{ console.log('2'); },0); console.log('3'); // Ex: 132 It is simple too, setTimeout function is asynchronous and JavaScript jump from it and after resolve run its function, so we see 2 after 1 and 3 . But, now I read async/await and I wrote a function like this: (async function test() { console

Is it safe to use async/await now? [closed]

时光毁灭记忆、已成空白 提交于 2019-12-03 10:27:14
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 2 years ago . Is it safe to use async-await in Javascript instead of generators-promises now, knowing that the syntax has not made yet and will be coming with the release of ES8? What browsers can I count on it being available, and how common are the browsers where this syntax is not

What's the actual use of the Atomics object in ECMAScript?

為{幸葍}努か 提交于 2019-12-03 05:48:40
The ECMAScript specification defines the Atomics object in the section 24.4 . Among all the global objects this is the more obscure for me since I didn't know about its existence until I didn't read its specification, and also Google hasn't many references to it (or maybe the name is too much generic and everything gets submerged?). According its official definition The Atomics object provides functions that operate indivisibly (atomically) on shared memory array cells as well as functions that let agents wait for and dispatch primitive events So it has the shape of an object with a number of

Is it safe to use async/await now? [closed]

£可爱£侵袭症+ 提交于 2019-12-03 05:13:50
Is it safe to use async-await in Javascript instead of generators-promises now, knowing that the syntax has not made yet and will be coming with the release of ES8? What browsers can I count on it being available, and how common are the browsers where this syntax is not available? By Safe I mean without some transpilers like babel? There are two places I check whenever I have questions such as this: The Can I Use website: http://caniuse.com/#search=await And Node Green : http://node.green/#async-functions Typically an answer is encouraged to include the relevant information to avoid link rot.