es6-promise

Why is synchronous sleep function not made async by being inside promise?

蓝咒 提交于 2019-12-01 21:58:57
I'm trying to wrap my head around promises, and how JavaScript works with it's queue and event loop etc. I thought that if I put a slow synchronous function inside a promise, that slow sync function would be delegated to the background and I could use .then to deal with it when it was done. function syncSleep(ms){ var end = new Date().getTime() + ms; var start = new Date().getTime(); while (start < end) { start = new Date().getTime(); } } function p() { return new Promise(function(resolve) { syncSleep(5000); resolve("syncSleep done!"); }); } p().then( function(s) { var div = document

My implementation of debounce axios request left the promise in pending state forever, is there a better way?

此生再无相见时 提交于 2019-12-01 21:26:28
I need a simple debounce function with immediate always true. Without resorting to lodash and with the help of Can someone explain the "debounce" function in Javascript , I implemented it as following, function debounce(func, wait) { var timeout; return function() { if (!timeout) func.apply(this, arguments); clearTimeout(timeout); timeout = setTimeout(()=>{timeout = null}, wait); }; }; It works as expected until I need to debounce axios request. Assumed I have a debounced axios method, I would like the calling method to be as usual, which means my debounced axios method should return promise I

Value of Promise.all() after it is rejected, shows [''PromiseStatus'']: resolved if catch block is present

天涯浪子 提交于 2019-12-01 18:25:28
I have two promises, one rejected and other resolved. Promise.all is called. It executed the catch block of Promise.all as one of the promises is rejected. const promise1 = Promise.resolve('Promise 1 Resolved'); const promise2 = Promise.reject('Promise 2 Rejected'); const promise3 = Promise.all([promise1, promise2]) .then(data => { console.log('Promise.all Resolved', data); }) .catch(error => { console.log('Promise.all REJECTED', error); }) setTimeout(() => { console.log(promise1, promise2, promise3) }, 200); If I don't have the catch on Promise.all(), the value remains as Rejected, ie const

Does “resolve” consistently mean something distinct from “fulfill”?

邮差的信 提交于 2019-12-01 17:06:48
问题 (Related but not quite the same: JS Promises: Fulfill vs Resolve) I've been trying to wrap my head around Javascript promises, and I'm struggling with the basic notions of resolve and resolved , vs. fulfill and fulfilled . I have read several introductions, such as Jake Archibald's, as well as browsing some relevant specs. In States and Fates (not quite an official spec, but referenced as an authoritative document written by one of the spec authors), fulfilled is a state while resolved is a

Angular 2 do not refresh view after array push in ngOnInit promise

坚强是说给别人听的谎言 提交于 2019-12-01 15:14:10
I created a NativeScript app with angular 2, i have an array of objects that i expect to see in the frontend of the application. the behaviour is that if i push an object into the array directly inside the ngOnInit() it works, but if i create a promise in the ngOnInit() it doesn't work. here is the code: export class DashboardComponent { stories: Story[] = []; pushArray() { let story:Story = new Story(1,1,"ASD", "pushed"); this.stories.push(story); } ngOnInit() { this.pushArray(); //this is shown var promise = new Promise((resolve)=>{ resolve(42); console.log("promise hit"); }); promise.then(x

How to .catch a Promise.reject

吃可爱长大的小学妹 提交于 2019-12-01 14:58:54
I have a helper function for using fetch with CouchDB which ends as: ... return fetch(...) .then(resp => resp.ok ? resp.json() : Promise.reject(resp)) .then(json => json.error ? Promise.reject(json) : json) and when I use it elsewhere, I was under the impression that I could .catch those explicit rejections: above_function(its_options) .then(do_something) .catch(err => do_something_with_the_json_error_rejection_or_resp_not_ok_rejection_or_the_above(err)) but alas, I can't seem to be able to get a hold of the rejections. The specific error I'm after is a HTTP 401 response. What gives? (Please

How can I export promise result?

时光总嘲笑我的痴心妄想 提交于 2019-12-01 14:29:21
问题 Sorry if this question is stupid. This code works correctly. And I just need to export data variable after all promises successfully resolved. I cannot put this code to function and export variable. Because in this case, this function will export an empty array. 'use strict' import urls from './urls' import getData from './get-data' getData(urls).then((responses) => { const data = [] const results = responses.map(JSON.parse) for (let i = 0, max = results.length; i < max; i++) { // some magic

How to .catch a Promise.reject

坚强是说给别人听的谎言 提交于 2019-12-01 13:45:52
问题 I have a helper function for using fetch with CouchDB which ends as: ... return fetch(...) .then(resp => resp.ok ? resp.json() : Promise.reject(resp)) .then(json => json.error ? Promise.reject(json) : json) and when I use it elsewhere, I was under the impression that I could .catch those explicit rejections: above_function(its_options) .then(do_something) .catch(err => do_something_with_the_json_error_rejection_or_resp_not_ok_rejection_or_the_above(err)) but alas, I can't seem to be able to

How to include both a parsed response and original response headers in fetch errors [duplicate]

梦想与她 提交于 2019-12-01 10:52:46
This question already has an answer here: How do I access previous promise results in a .then() chain? 17 answers I have the following promise chain: return fetch(request) .then(checkStatus) .then(response => response.json()) .then(json => ({ response: json })) .catch(error => ({ error })) Where checkstatus() checks if the request was successful, and returns an error if it wasn't. This error will be caught and returned. But, the problem is that I want to add the both response.statusText and the results of response.json() to the error. The problem is that when I parse it I lose the original

Executing a function after all async functions have completed?

元气小坏坏 提交于 2019-12-01 10:47:46
问题 this.validate_label_population(); this.validate_title_prefix(); this.validate_title_suffix(); this.executeGitCommentCreation(); I have the following functions executing in a constructor. The top 3/4 are async functions: Example: async validate_title_prefix() { console.log('validate_title_prefix not implemented'); } I want to execute this.executeGitCommentCreation(); last after al the previous have ran. What is the best way to do this? Should I throw await in front of the top 3, or use some