ecmascript-2017

(ES6) class (ES2017) async / await getter

試著忘記壹切 提交于 2019-11-30 10:41:53
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 someAsyncOperation() { return new Promise(function(resolve) { setTimeout(function() { resolve('baz'); }, 1000); }); } var foo = new Foo(); foo.bar.should.equal('baz'); You can do this class Foo { get bar() { return (async () => { return await someAsyncOperation(); })(); } } which again is the same as class Foo { get bar() { return new Promise((resolve, reject) => { someAsyncOperation()

Async / await assignment to object keys: is it concurrent?

有些话、适合烂在心里 提交于 2019-11-30 09:12:54
问题 I know that doing this: const resultA = await a() const resultB = await b() // code here Is effectively a().then( resultA => { b().then( resultB => { // code here }) }) Basically, a() runs then b() runs. I nested them to show that both resultA and resultB are in our scope; yet both function didn't run at once. But what about this: const obj = { result1: await a(), result2: await b() } do a() and b() run concurrently? For reference: const asyncFunc = async (func) => await func.call() const

Is it possible to use await without async in Js

我的未来我决定 提交于 2019-11-29 18:26:13
问题 Await is a amazing feature in es7. However,everytime I use await I found that I have to define a async function and call this function. Such as async function asy(){ const [resCityGuess,resCityHot,resCityAll]=await Promise.all([ this.http.get('api/v1/cities?type=guess'), this.http.get('api/v1/cities?type=hot'), this.http.get('api/v1/cities?type=group') ]) this.cityGuessName=resCityGuess.data.name; this.cityGuessId=resCityGuess.data.id; this.cityHot=resCityHot.data; this.cityAll=resCityAll

running queries in firebase using async / await

好久不见. 提交于 2019-11-29 16:27:14
问题 Appreciating that firebase has added support for promises, is there a way to run a query like the following inside of an async function?: const eventref = this.db.ref('cats/whiskers'); const value = await eventref.once('value') Running the above returns a promise for value , I'm hoping to get the json blob that is stored at cats/whiskers . 回答1: The result of value is a snapshot, we need 1 more step to get the value. This should be like: const eventref = this.db.ref('cats/whiskers'); const

Async / await assignment to object keys: is it concurrent?

泪湿孤枕 提交于 2019-11-29 14:06:17
I know that doing this: const resultA = await a() const resultB = await b() // code here Is effectively a().then( resultA => { b().then( resultB => { // code here }) }) Basically, a() runs then b() runs. I nested them to show that both resultA and resultB are in our scope; yet both function didn't run at once. But what about this: const obj = { result1: await a(), result2: await b() } do a() and b() run concurrently? For reference: const asyncFunc = async (func) => await func.call() const results = [funcA,funcB].map( asyncFunc ) I know here funcA and funcB do run concurrently. Bonus : How

ES8 Immediately invoked async function expression

耗尽温柔 提交于 2019-11-29 03:44:30
I haven't seen these constructs used much but I've found myself writing them to make use of async / await in functions that wouldn't typically return a promise, for example chan.consume(queue, (msg) => { this.pendingMsgs++; // executed immediately (async () => { await this.handleMessage(msg); this.pendingMsgs--; if (cancelled && this.pendingMsgs === 0) { await chan.close(); await this.amqpConnectionPool.release(conn); } })(); }); as opposed to chan.consume(queue, async (msg) => { // external lib does not expect a return value from this callback this.pendingMsgs++; // executed in promise

Use Async/Await with Axios in React.js

梦想的初衷 提交于 2019-11-28 18:54:15
问题 Following How to use async/await with axios in react I am trying to make a simple get request to my server using Async/Await in a React.js App. The server loads a simple JSON at /data which looks like this JSON { id: 1, name: "Aditya" } I am able to get the data to my React App using simple jquery ajax get method. However, I want to make use of axios library and Async/Await to follow ES7 standards. My current code looks like this: class App extends React.Component{ async getData(){ const res

Is async await truly non-blocking in the browser?

☆樱花仙子☆ 提交于 2019-11-28 17:14:47
I have been playing around with the feature in an SPA using TypeScript and native Promises, and I notice that even if I refactor a long-running function into an async function returning a promise, the UI is still unresponsive. So my questions are: How exactly does the new async/await feature help avoid blocking the UI in the browser? Are there any special extra steps one needs to take when using async/await to actually get a responsive UI? Can someone create a fiddle to demonstrate the how async/await helps to make the UI responsive? How does async/await relate to prior async features such as

Using async/await with a forEach loop

柔情痞子 提交于 2019-11-28 12:39:38
问题 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

How to run Node.js app with ES2017 features enabled on Heroku?

流过昼夜 提交于 2019-11-28 12:13:20
I'm new to Node and created an app that has some async/await syntax in it like so: const express = require('express'); const app = express(); const someLibrary = require('someLibrary'); function asyncWrap(fn) { return (req, res, next) => { fn(req, res, next).catch(next); }; }; app.post('/getBlock', asyncWrap(async (req,res,next) => { let block = await someLibrary.getBlock(req.body.id); [some more code] })); app.listen(process.env.PORT || 8000); It works fine on my machine but when I deploy to Heroku I get an error because the syntax is not supported: 2017-03-23T10:11:13.953797+00:00 app[web.1]