es6-promise

Differences between Promise.all() and Promise.allSettled() in JS?

老子叫甜甜 提交于 2020-07-03 06:06:57
问题 I was reading the MDN 's manual on Promise , and I found these two methods which seem similar to me: Promise.allSettled(iterable); Promise.all(iterable); Both of them take an iterable and return an array containing the fulfilled Promise s. So, what is the difference between them? 回答1: Promise.all will reject as soon as one of the Promises in the array rejects. Promise.allSettled will never reject - it will resolve once all Promises in the array have either rejected or resolved. Their resolve

Differences between Promise.all() and Promise.allSettled() in JS?

巧了我就是萌 提交于 2020-07-03 06:05:12
问题 I was reading the MDN 's manual on Promise , and I found these two methods which seem similar to me: Promise.allSettled(iterable); Promise.all(iterable); Both of them take an iterable and return an array containing the fulfilled Promise s. So, what is the difference between them? 回答1: Promise.all will reject as soon as one of the Promises in the array rejects. Promise.allSettled will never reject - it will resolve once all Promises in the array have either rejected or resolved. Their resolve

Firebase web: upload multiple files to Storage an then download their URLs

混江龙づ霸主 提交于 2020-06-29 04:32:13
问题 I am creating a blogging website, and am writing code that does the following in order: 1. stores multiple photos that the user uploads 2. download their URLs 3. save them to the realtime database. I wrote the function below to do #1 and #2. Basically the idea is to store all the urls to an array, in this case 'urlarray'. function url_array_get(){ return new Promise(function(resolve,reject){ let filez=review_photo.files; let urlarray=[]; let user=firebase.auth().currentUser; let files=Array

What does it mean for promises to be immutable and their guaranteed value?

感情迁移 提交于 2020-06-28 04:41:32
问题 I'm trying to understand the differences between es6 promises and regular callbacks but don't get the examples below. Can someone show what it would look like to do the below with callbacks? // an immediately resolved promise var p2 = Promise.resolve("foo"); // can get it after the fact, unlike events p2.then((res) => console.log(res)); var p = new Promise(function(resolve, reject) { setTimeout(() => resolve(4), 2000); }); // handler can't change promise, just value p.then((res) => { res += 2

Is the Promise constructor synchronous? [duplicate]

烈酒焚心 提交于 2020-06-27 16:25:47
问题 This question already has answers here : Is the Promise constructor callback executed asynchronously? (2 answers) When is the body of a Promise executed? (5 answers) Closed 6 months ago . Say the code in a Promise constructor contains synchronous assignments. I have something like this: function x() { let rejector = null; new Promise((resolve, reject) => { rejector = reject; // async code follows - setTimeout, network requests, etc. }); return rejector; } Is the synchronous code in the

Property 'allSettled' does not exist on type 'PromiseConstructor'.ts(2339)

你说的曾经没有我的故事 提交于 2020-06-25 09:17:07
问题 I try to use Promise.allSettled API with TypeScript. Code here: server.test.ts : it('should partial success if QPS > 50', async () => { const requests: any[] = []; for (let i = 0; i < 51; i++) { requests.push(rp('http://localhost:3000/place')); } await Promise.allSettled(requests); // ... }); But TSC throws an error: Property 'allSettled' does not exist on type 'PromiseConstructor'.ts(2339) I already added these values to lib option of tsconfig.json : tsconfig.json : { "compilerOptions": { /*

When NOT to use promises [closed]

梦想的初衷 提交于 2020-06-24 05:09:13
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 4 years ago . Improve this question After having read dozens of articles on how great es6 promises are and why we should implement them I have been left with the feeling that ALL of my (non-trivial) javascript functions should be promises. In fact I feel great when writing code using them

Save Async/Await response on a variable

落花浮王杯 提交于 2020-06-24 04:50:11
问题 I am trying to understand async calls using async/await and try/catch. In the example below, how can I save my successful response to a variable that can be utilized throughout the rest of the code? const axios = require('axios'); const users = 'http://localhost:3000/users'; const asyncExample = async () =>{ try { const data = await axios(users); console.log(data); //200 } catch (err) { console.log(err); } }; //Save response on a variable const globalData = asyncExample(); console.log

Async/Await in Express Middleware

落花浮王杯 提交于 2020-06-17 01:52:33
问题 I'm having trouble understanding how to properly write middleware in Express that utilizes async/await, but doesn't leave a Promise floating in the ether after it's execution. I've read a ton of blogs and StackOverflow posts, and it seems like there is some consensus around using the following pattern in async/await middleware: const asyncHandler = fn => (req, res, next) => Promise .resolve(fn(req, res, next)) .catch(next) app.use(asyncHandler(async (req, res, next) => { req.user = await User

Get a value inside a Promise Typescript

穿精又带淫゛_ 提交于 2020-06-14 04:20:06
问题 One of function inside a typescript class returns a Promise<string> . How do I unwrap/yield the value inside that promise. functionA(): Promise<string> { // api call returns Promise<string> } functionB(): string { return this.functionA() // how to unwrap the value inside this promise } 回答1: How do I unwrap/yield the value inside that promise You can do it with async / await .Don't be fooled into thinking that you just went from async to sync, async await it is just a wrapper around .then .