es6-promise

ReactJS setState when all nested Axios calls are finished

我的梦境 提交于 2020-05-14 12:16:04
问题 I have a problem with updating my state from nested axios call inside forEach loop: constructor(props) { super(props); this.state = { isLoaded: false, items: [] }; //Binding fetch function to component's this this.fetchFiles = this.fetchFiles.bind(this); } componentDidMount() { this.fetchFiles(); } fetchFiles() { axios.get('/list') .then((response) => { var items = response.data.entries; items.forEach((item, index) => { axios.get('/download'+ item.path_lower) .then((response) => { item.link =

Javascript, Promises and setTimeout

╄→гoц情女王★ 提交于 2020-05-13 07:34:52
问题 I have been playing with Promises, but I am having trouble understanding what is happening with the following code: const promise = new Promise((resolve, reject) => { console.log('Promise started - Async code started') setTimeout(() => { resolve('Success') }, 10) }) setTimeout(() => { console.log('Promise log inside first setTimeout') }, 0) promise.then(res => { console.log('Promise log after fulfilled') }) console.log('Promise made - Sync code terminated') setTimeout(() => { console.log(

How to bubble a web worker error in a promise via worker.onerror?

烈酒焚心 提交于 2020-05-11 07:57:56
问题 I'm dealing with a web worker that needs to report back if an error has occured. Normally, I can use worker.onerror to listen to any errors that were thrown from the worker. However, when the error happens in a promise in the web worker, I can't figure out how to bubble the error back up to worker.onerror . Excuse the odd code snippet, as this seems to be the only way to demonstrate worker behavior. (The worker is defined in the HTML section). function getInlineJS() { var js = document

How to bubble a web worker error in a promise via worker.onerror?

痴心易碎 提交于 2020-05-11 07:57:31
问题 I'm dealing with a web worker that needs to report back if an error has occured. Normally, I can use worker.onerror to listen to any errors that were thrown from the worker. However, when the error happens in a promise in the web worker, I can't figure out how to bubble the error back up to worker.onerror . Excuse the odd code snippet, as this seems to be the only way to demonstrate worker behavior. (The worker is defined in the HTML section). function getInlineJS() { var js = document

Difference between returning new Promise and Promise.resolve

故事扮演 提交于 2020-05-11 06:28:40
问题 For the below code snippet, i would like to understand how NodeJS runtime handles things : const billion = 1000000000; function longRunningTask(){ let i = 0; while (i <= billion) i++; console.log(`Billion loops done.`); } function longRunningTaskProm(){ return new Promise((resolve, reject) => { let i = 0; while (i <= billion) i++; resolve(`Billion loops done : with promise.`); }); } function longRunningTaskPromResolve(){ return Promise.resolve().then(v => { let i = 0; while (i <= billion) i++

How make promise execute synchronously?

独自空忆成欢 提交于 2020-05-11 04:58:42
问题 I use dom-to-image.js for converting dom to png image. As dom-to-image.js uses promise, the code executes asynchronously. I want to execute .then function synchronously. I have the following code: domtoimage.toPng(document.getElementById("main")).then(function(dataUrl) { console.log(dataUrl); }).catch(function(error) { console.error('oops, something went wrong!', error); }); console.log("this console should be executed after console.log(dataUrl)") I want to execute .then function first,

How make promise execute synchronously?

余生长醉 提交于 2020-05-11 04:57:07
问题 I use dom-to-image.js for converting dom to png image. As dom-to-image.js uses promise, the code executes asynchronously. I want to execute .then function synchronously. I have the following code: domtoimage.toPng(document.getElementById("main")).then(function(dataUrl) { console.log(dataUrl); }).catch(function(error) { console.error('oops, something went wrong!', error); }); console.log("this console should be executed after console.log(dataUrl)") I want to execute .then function first,

How long a Promise can remain in pending state?

元气小坏坏 提交于 2020-04-10 09:26:19
问题 I'm using Promises in Angular (4) project and I have a question about them that I couldn't find a response to it in the docs. When I create a Promise, I basically wait for an async answer from a service/party. But how long should I expect this Promise to stay in pending state? Is there any mechanism that will terminate it after a while? How reliable is this concept of waiting/pending? Let's suppose that I need to get some data from a busy service that can answer even after few minutes of

What does the operation HostPromiseRejectionTracker do?

和自甴很熟 提交于 2020-03-25 21:30:33
问题 I looked at HostPromiseRejectionTracker in the ECMAScript specification, but still did not understand what it was doing. It does not have specific steps of the algorithm, so it is not clear how this operation works in the current code. One thing is clear that HostPromiseRejectionTracker is called when creating a new Promise when executing a function that calls the reject function. And the second time when "then" method is called for the first time, HostPromiseRejectionTracker is called only

Can't set state in react

烂漫一生 提交于 2020-03-25 13:41:21
问题 So, I'm simply trying to set state in my react app. Simply get data from Axios, and then set state. But no matter what I do, the state will not set. I've tried putting it in a callback since it's async and putting it my component did mount and component did update alas nothing. any pointers? class App extends Component { componentDidUpdate() {} constructor(props) { super(props); this.state = { Catogories: [ "Business", "Entertainment", "General", "Health", "Science", "Sports", "Technology" ],