ecmascript-2017

Use Async/Await with Axios in React.js

不想你离开。 提交于 2019-12-02 17:40:23
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 = await axios('/data'); console.log(res.json()); } render(){ return( <div> {this.getData()} </div> ); }

How does `for..of` loop resolve the iterator from an object?

喜欢而已 提交于 2019-12-01 19:45:34
For an object to implement iterable interface it must implement [Symbol.iterator] key that points to a function that returns the iterator . I'm wondering if the for..of loop internally calls this method on an object to get that iterator ? The reason I'm curious is that, for example, Map defines an interface with several iterators (entries, values, keys) and it seems that if not specified explicitly the for..of loop uses the iterator returned by map.entries() call. I've trying searching in the specification but it only specifies that iterator is passed as a parameter to the abstract operation

Babel ESLint: TypeError: Cannot read property 'range' of null

删除回忆录丶 提交于 2019-12-01 16:29:32
I use babel-eslint to lint/fix my code. Worked great until I wanted to adopt some ES2017 async await found overhere . I changed my React app accordingly, allbeit slightly different: The relevant part of my index.js: async function renderApp() { const store = await configureStore() const history = syncHistoryWithStore(browserHistory, store, { selectLocationState: state => state.get('routing') }) ReactDOM.render( <AppContainer> <MuiThemeProvider muiTheme={muiTheme}> <Provider store={store}> <Router history={history} routes={routes(store)} /> </Provider> </MuiThemeProvider> </AppContainer>,

Babel ESLint: TypeError: Cannot read property 'range' of null

倾然丶 夕夏残阳落幕 提交于 2019-12-01 15:31:04
问题 I use babel-eslint to lint/fix my code. Worked great until I wanted to adopt some ES2017 async await found overhere. I changed my React app accordingly, allbeit slightly different: The relevant part of my index.js: async function renderApp() { const store = await configureStore() const history = syncHistoryWithStore(browserHistory, store, { selectLocationState: state => state.get('routing') }) ReactDOM.render( <AppContainer> <MuiThemeProvider muiTheme={muiTheme}> <Provider store={store}>

Catch Geolocation Error - Async Await

一曲冷凌霜 提交于 2019-12-01 05:42:52
How can I catch the geolocation specific error to notify the user that they must have geolocation turned on? The catch logs an error called PositionError as referenced here in the Mozilla docs " https://developer.mozilla.org/en-US/docs/Web/API/PositionError ". *Note: my code does not catch the error, it simply displays: Uncaught (in promise) ReferenceError: PositionError is not defined Code getCurrentLocation() { return new Promise((resolve, reject) => { navigator.geolocation.getCurrentPosition(resolve, reject, { enableHighAccuracy: true, timeout: 5000, maximumAge: 0 }); }); }, async inout() {

Can I detect async/await available in browser? [duplicate]

社会主义新天地 提交于 2019-12-01 04:38:39
This question already has an answer here: How to detect async function support without eval? 1 answer As title, how can I detect async/await es7 support in browser? Is that possible? As any other syntactic feature, it should be evaluated in order to be detected. Since eval can be restricted, this may be impossible when CSP is enabled: let isAsync = true; try { eval('async () => {}'); } catch (e) { if (e instanceof SyntaxError) isAsync = false; else throw e; // throws CSP error } If there's a chance that target browsers don't support a feature, the code should be transpiled. The alternative

Can I detect async/await available in browser? [duplicate]

六眼飞鱼酱① 提交于 2019-12-01 02:54:02
问题 This question already has an answer here : How to detect async function support without eval? (1 answer) Closed 2 years ago . As title, how can I detect async/await es7 support in browser? Is that possible? 回答1: As any other syntactic feature, it should be evaluated in order to be detected. Since eval can be restricted, this may be impossible when CSP is enabled: let isAsync = true; try { eval('async () => {}'); } catch (e) { if (e instanceof SyntaxError) isAsync = false; else throw e; //

async function - await not waiting for promise

故事扮演 提交于 2019-12-01 02:52:45
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) { resolve('State is true'); } else { reject('State is false'); } }, 3000); }); } const getResult = async () => { return await myFun(); } console.log(getResult()); why am I getting output as - Promise { <pending> } Instead of some value? Shouldn't the getResult() function wait for myFun() function resolve it's promise value? If you're using async/await, all your calls have to use Promises or async

Catch Geolocation Error - Async Await

牧云@^-^@ 提交于 2019-12-01 01:30:00
问题 How can I catch the geolocation specific error to notify the user that they must have geolocation turned on? The catch logs an error called PositionError as referenced here in the Mozilla docs "https://developer.mozilla.org/en-US/docs/Web/API/PositionError". *Note: my code does not catch the error, it simply displays: Uncaught (in promise) ReferenceError: PositionError is not defined Code getCurrentLocation() { return new Promise((resolve, reject) => { navigator.geolocation.getCurrentPosition

running queries in firebase using async / await

不打扰是莪最后的温柔 提交于 2019-11-30 11:05:29
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 . 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 snapshot = await eventref.once('value'); const value = snapshot.val(); 来源: https://stackoverflow.com/questions