es6-promise

Why does JavaScript's `Promise.all` not run all promises in failure conditions?

寵の児 提交于 2019-12-18 04:16:08
问题 According to MDN: If any of the passed in promises rejects, the all Promise immediately rejects with the value of the promise that rejected, discarding all the other promises whether or not they have resolved. The ES6 spec seems to confirm this. My question is: Why does Promise.all discard promises if any of them reject, since I would expect it to wait for "all" promises to settle , and what exactly does "discard" mean? (It's hard to tell what "discard" means for in-flight promises vs.

Promises With Google Maps Geocoder API

痞子三分冷 提交于 2019-12-18 03:41:55
问题 Im trying to create a set of functions that translates a set of addresses to lat long values using the Google Maps Geocoder API. Currently, I have it successfully translating the addresses to lat long values but the function fully executes before theyre returned. I know this because it throws undefined errors before it logs the proper lat long values after. I heard that javascripts promises can be a solution to this type of problem so I did a little bit of research but it doesn't seem to be

Node.js: When to use Promises vs Callbacks

▼魔方 西西 提交于 2019-12-17 23:36:54
问题 I have some older Node.js code that I'm updating. In the process I'm designing new modules to work with the old code. I'm finding that now, as opposed to when I first wrote this, I rely more on using ES6 promises rather than callbacks. So now I have this mix of some functions returning promises and some taking callbacks - which is tedious. I think eventually it should be refactored to use promises. But before that is done... What are the situations where promises are preferred and where are

fromPromise does not exist on type Observable

一曲冷凌霜 提交于 2019-12-17 23:06:34
问题 In Angular 2 using rxjs I was trying to convert a Promise to Observable. As many of online guides showed I used fromPromise on Observable . Which throws error: Property 'fromPromise' does not exist on type 'typeof Observable'. Observable was imported like: import { Observable } from "rxjs/Observable"; trying to import fromPromise like other operators results in error: import 'rxjs/add/operator/fromPromise'; even if I suppress typescript error it still results in error: (<any>Observable)

Why does javascript ES6 Promises continue execution after a resolve?

戏子无情 提交于 2019-12-17 22:29:26
问题 As I understand a promise is something that can resolve() or reject() but I was suprised to find out that code in the promise continues to execute after a resolve or reject is called. I considered resolve or reject being an async-friendly version of exit or return , that would halt all immediate function execution. Can someone explain the thought behind why the following example sometimes shows the console.log after a resolve call: var call = function() { return new Promise(function(resolve,

using promises in node.js to create and compare two arrays

故事扮演 提交于 2019-12-17 21:21:50
问题 I needed to compare two arrays the first one a couple of filenames from a database, the second one a list of files I already downloaded to my client. The Idea was to load whatever files are missing on the client. As the reading via fs was two slow, I tried using Promises to wait for one function to finish before the next starts. But somehow I got lost... My code so far: let filesIneed = []; let filesIhave = []; let filesToFetch = []; getLocalFiles().then(getFilesIneed).then(getfilesToRetreive

What should happen if you resolve a promise with another promise?

我的梦境 提交于 2019-12-17 21:19:56
问题 What does the ECMAScript 6.0 specification say about a Promise resolving another Promise ? Should it adopt the state of the other Promise by attaching a then to that Promise which would resolve this one? I tried this snippet in Chrome and here is what I get and it seems to just resolve the Promise1 with Promise2 is that fine? > Promise1 = new Promise(function(resolve){ResolveYouLater1 = resolve}) > Promise2 = new Promise(function(resolve){ResolveYouLater2 = resolve}) > ResolveYouLater1

Why does the Promise object block rendering?

落花浮王杯 提交于 2019-12-17 21:15:04
问题 I was testing the Promise object and wrote some code that simulates a long running task that is synchronous. I was comparing Promise and setTimeout - see fiddle: <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="style.css"> </head> <body> <h2>Promise vs setTimeout</h2> <div><button id="settimeout-test">setTimeout with slow running function</button></div> <div><button id="promise-test">Promise and slow running function</button></div> <div><button id="clear">Clear Results</button></div

How to create a function that returns an existing promise instead of new promise?

自古美人都是妖i 提交于 2019-12-17 20:58:10
问题 JavaScript/Promise experts, I hope you can help me, because I don't understand how I can create a function that returns an existing promise instead of a new promise. I have done a lot of research, but all examples return a new promise every time the function is called. Assume I have a function that takes some time to process and generates a different result every time it is being called. When I use the new promise method then I still need to wait to complete every time and I don't get the

Why does JavaScript Promise then handler run after other code?

心不动则不痛 提交于 2019-12-17 17:10:03
问题 I'm just trying to improve my understanding on how JavaScript Promises work. I've created the following situation: LOG 'FOO' RUN CALLBACK LOGGING 'CALLBACK' LOG 'BAR' Expect all functions to complete immediately (by this I mean they will not take an excessive/unknown amount of time to complete that you would use an async operation to complete) so that the above order of operations will happen in that order. You can write this in the following way: function foo(cb) { // LOG 'FOO' console.log(