ecmascript-2017

ES2017 - Async vs. Yield

混江龙づ霸主 提交于 2019-11-28 07:13:43
I am confused about the current discussion of adding async functions and the keyword await to the next EcmaScript. I do not understand why it is necessary to have the async keyword before the function keyword. From my point of view the await keyword to wait for a result of a generator or promise done , a function's return should be enough. await should simple be usable within normal functions and generator functions with no additional async marker. And if I need to create a function what should be usable as an result for an await , I simply use a promise. My reason for asking is this good

Is using async in setTimeout valid?

北战南征 提交于 2019-11-28 06:31:15
I had a asynchronous function in Javascript and I added setTimeout to it. The code looks like that: let timer; clearTimeout(timer); timer =setTimeout(() => { (async() => { await this._doSomething(); })(); }, 2000); The puprose of setTimeout is to add 2 seconds before function will be run. It is to be sure that user stopped typing. Should I remove async/await from this function now, since setTimeout is asynchronous anyway? Any help here much appreciated! setTimeout adds a delay before a function call, whereas async / await is syntactic sugar ontop of promises, a way to chain code to run after a

Async/Await inside Array#map()

…衆ロ難τιáo~ 提交于 2019-11-27 20:03:13
I'm getting compile time error with this code: const someFunction = async (myArray) => { return myArray.map(myValue => { return { id: "my_id", myValue: await service.getByValue(myValue); } }); }; Error message is: await is a reserved word Why can't I use it like this? I also tried another way, but it gives me same error: const someFunction = async (myArray) => { return myArray.map(myValue => { const myNewValue = await service.getByValue(myValue); return { id: "my_id", myValue: myNewValue } }); }; You can't do this as you imagine, because you can't use await if it is not directly inside an

how to async/await redux-thunk actions?

て烟熏妆下的殇ゞ 提交于 2019-11-27 14:24:21
问题 action.js export function getLoginStatus() { return async(dispatch) => { let token = await getOAuthToken(); let success = await verifyToken(token); if (success == true) { dispatch(loginStatus(success)); } else { console.log("Success: False"); console.log("Token mismatch"); } return success; } } component.js componentDidMount() { this.props.dispatch(splashAction.getLoginStatus()) .then((success) => { if (success == true) { Actions.counter() } else { console.log("Login not successfull"); } });

How to use ES8 async/await with streams?

↘锁芯ラ 提交于 2019-11-27 11:54:30
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 fd = fs.createReadStream('/some/file/name.txt'); var hash = crypto.createHash('sha1'); hash.setEncoding('hex'); fd.on('end', function() { hash.end(); console.log(hash.read()); // the desired sha1sum }); // read all file and pipe it (write it) to the hash object fd.pipe(hash); But is it possible to convert this to using ES8 async/await instead of using the

Is async await truly non-blocking in the browser?

蹲街弑〆低调 提交于 2019-11-27 10:08:00
问题 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

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

我们两清 提交于 2019-11-27 06:50:50
问题 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

Is using async in setTimeout valid?

百般思念 提交于 2019-11-27 05:41:27
问题 I had a asynchronous function in Javascript and I added setTimeout to it. The code looks like that: let timer; clearTimeout(timer); timer =setTimeout(() => { (async() => { await this._doSomething(); })(); }, 2000); The purpose of setTimeout is to add 2 seconds before function will be run. It is to be sure that user stopped typing. Should I remove async/await from this function now, since setTimeout is asynchronous anyway? 回答1: setTimeout adds a delay before a function call, whereas async /

async/await always returns promise

半腔热情 提交于 2019-11-27 04:47:10
I'm trying async/await functionality. I have such code imitating a request: const getJSON = async () => { const request = () => new Promise((resolve, reject) => ( setTimeout(() => resolve({ foo: 'bar'}), 2000) )); const json = await request(); return json; } When I use the code in this way console.log(getJSON()); // returns Promise it returns a Promise but when I call this line of code getJSON().then(json => console.log(json)); // prints { foo: 'bar' } it prints json as expected Is it possible to use just code like console.log(getJSON()) ? What don't I understand? Every async function returns

Is there a way to wrap an await/async try/catch block to every function?

夙愿已清 提交于 2019-11-27 01:53:11
So i'm using express.js and looking into using async/await with node 7. Is there a way that I can still catch errors but get rid of the try/catch block? Perhaps a function wrapper? I'm not sure how this would actually execute the function's code and also call next(err) . exports.index = async function(req, res, next) { try { let user = await User.findOne().exec(); res.status(200).json(user); } catch(err) { next(err); } } Something like this...? function example() { // Implements try/catch block and then handles error. } exports.index = async example(req, res, next) { let user = await User