ecmascript-next

async / await not working in combination with fetch

巧了我就是萌 提交于 2019-11-29 17:08:52
问题 I'm trying to use ES7 async / await together with fetch . I know I'm close but I can't get it to work. Here is the code: class Bar { async load() { let url = 'https://finance.yahoo.com/webservice/v1/symbols/goog/quote?format=json'; try { response = await fetch(url); return response.responseText; } catch (e) { return e.message; } } } which I use as follows: let bar = new Bar(); bar.load().then(function (val) { console.log(val); }); DEMO For some reason I always get into the catch with the

ES6+ instance property instantiated outside of constructor

我的梦境 提交于 2019-11-29 14:47:53
Using ES6+ syntax in React/React-Native, the variable foo , when defined outside of the constructor is somehow transformed into an instance variable when called with this. . Is my assertion true? Why does it even work, when not instantiated in the constructor? Here a corresponding React Native code snippet: class myComponent extends Component { constructor() { super(); } foo = "bar"; render() { return ( <View>{ this.foo }</View> ); } } This discussion about ES7 property initializers shows how the state variable is prominently used in this way in React/React Native. So far related Stack

Getting value from returned promise from async function

半世苍凉 提交于 2019-11-29 13:40:53
I'm getting used to the proposed async/await syntax and there's some unintuitive behavior. Inside the "async" function, I can console.log the correct string. However, when I try to return that string, instead, it returns a promise. Checking this entry: async/await implicitly returns promise? , it's pretty clear that any "async function()" will return a promise, NOT a value. That's fine. But how do you get access to the value? If the only answer is "a callback", that's fine - but I was hoping there might be something more elegant. // src // ========================================== require(

Is there a way to short circuit async/await flow?

萝らか妹 提交于 2019-11-28 19:46:31
问题 async function update() { var urls = await getCdnUrls(); var metadata = await fetchMetaData(urls); var content = await fetchContent(metadata); await render(content); return; } //All the four functions return a promise. (getCdnUrls, fetchMetaData, fetchContent, render) What if we want to abort the sequence from outside, at any time ? Say, when fetchMetaData is being executed, we realize the component is no longer needed to be rendered and we want to cancel the remaining operations

Error using async/await in React Native

大兔子大兔子 提交于 2019-11-28 17:45:00
When trying to use async/await in react-native, I am getting the following error: uncaught error Error: SyntaxError: /Users/senthilsivanath/Documents/MusicTulip/index.ios.js: Unexpected token (50:23) 48 | renderScene: function(route,nav) { 49 | try { 50 | const response = await signIn.isLoggedIn(); My .babelrc file is: { "presets": ["react-native", "es2015", "babel-preset-stage-3"] } You might just be missing the async keyword on line 48. Update your code to use the async keyword before the function keyword: renderScene: async function(route, nav) { try { const response = await signIn

Javascript ES6+: Destructuring and using an array method at the same time?

只谈情不闲聊 提交于 2019-11-28 14:37:40
I was wondering if there is a way, to destructe and use an array method at the same time? If yes, is it useful to use it, or would it decrease the code readabilty so much, that it's not worth it? My current code is this: const { props: { title, ingredients: ing } } = this; const ingredients = ing.map( (ing, index) => <li key={index}>{ing}</li> ); But I'm trying to find a shorter way like this: const { props: { title, ingredients: ingredients.map( (ing, index) => <li key={index}>{ing}</li> ); } } = this; This code doesn't work though. Any tips would be much appreciated! :) No, this is not

Why does assignment using await within while expression not retain assigned value?

二次信任 提交于 2019-11-28 14:15:40
问题 Given the JavaScript code function* gen(start = 0, stop = 5) { while (start < stop) yield ++start; } async function fn(g = gen()) { while (done = !await new Promise(resolve => setTimeout(resolve, 1000, g.next())) .then(({value, done}) => { // `value:undefined, done:true` following // `value:5, done:false` console.log(`value:${value}, done:${done}`); return done } , err => Promise.reject(err))); return done; // what happened to the `true` value assigned? } // why is `res` `false`? fn().then

ES6+ instance property instantiated outside of constructor

試著忘記壹切 提交于 2019-11-28 08:42:06
问题 Using ES6+ syntax in React/React-Native, the variable foo , when defined outside of the constructor is somehow transformed into an instance variable when called with this. . Is my assertion true? Why does it even work, when not instantiated in the constructor? Here a corresponding React Native code snippet: class myComponent extends Component { constructor() { super(); } foo = "bar"; render() { return ( <View>{ this.foo }</View> ); } } This discussion about ES7 property initializers shows how

Why await is not working for node request module?

Deadly 提交于 2019-11-28 06:15:29
I'm new to nodejs. I’m not seeing the response in ex 1, but i see in ex 2. Why? Await works for me in other places, using babel. Ex 1 let res = await request(url) console.log(res); console.log(res.body); Ex 2 request(url, function (error, res, body) { if (!error && response.statusCode == 200) { console.log(body) } }); Await works in other places, I’m using babel and required modules for es6 and es7 features. For example, await works in squelize call, i validated. But it doesn’t work for request call. Why? You should only await on something that returns a Promise. I would definitely recommend

In ES2015 how can I ensure all methods wait for object to initialize ? With ES7 decorators?

谁都会走 提交于 2019-11-28 05:36:10
问题 I have an ES2015 class that connects to a remote service. The problem is that my code tries to access this class before its object has finished connecting to the remote server. I want to ensure that methods don't just give an error if the object has not finished initializing. I'll have alot of methods in my class that depend on the connection being up and running, so it would be good if there was a single, easy to understand mechanism that could be applied to all methods like an