es6-promise

JS Promises: is doing `return(value)` in a `then` block the same as resolving?

北城余情 提交于 2021-02-08 11:58:17
问题 I have the following code: new Promise((resolve, reject) => { resolve(1) }).then(value => { return 2 }) I resolve the initial promise with 1 . Then in the then block I do return 2 . Does this return a promise resolved with the value 2? 回答1: Yes. Calling .then creates a new promise, and that promise will resolve to whatever you return in the callback. 来源: https://stackoverflow.com/questions/61510453/js-promises-is-doing-returnvalue-in-a-then-block-the-same-as-resolving

How to turn Firestore query into a Javascript array

Deadly 提交于 2021-02-08 05:16:30
问题 I am trying to export a firestore function that performs a query and returns an array containing the objects in that query. I am trying to get data from a subcollection of a document, and get an array of document objects returned to render to the client. I've tried the below but it's not working (e.g. the object returns blank). I think this has to do with improper handling of promises, but couldn't figure it out on my own. Thanks for your help. export const getEvents = (id) => { let events =

catching errors in typescript promises

时光怂恿深爱的人放手 提交于 2021-02-07 13:49:31
问题 Angular2 has very useful promises error catching mechanism for chained promises. Yet, the usual case (at least for me) is that of promises called from within the resolve handler of the previous one. This is due to the need to process information prior to starting the next promise. For example: this.d( "facebookOAuthLogin() - starts" ); this.fbProvider.login().then( ( loginResponse: { status: string, authResponse: any, accessToken: string, expiresIn: string, session_key: string, sig: string,

catching errors in typescript promises

孤街醉人 提交于 2021-02-07 13:47:39
问题 Angular2 has very useful promises error catching mechanism for chained promises. Yet, the usual case (at least for me) is that of promises called from within the resolve handler of the previous one. This is due to the need to process information prior to starting the next promise. For example: this.d( "facebookOAuthLogin() - starts" ); this.fbProvider.login().then( ( loginResponse: { status: string, authResponse: any, accessToken: string, expiresIn: string, session_key: string, sig: string,

What makes `async/await` statements run sequentially vs in parallel in ES6?

我只是一个虾纸丫 提交于 2021-02-07 11:20:15
问题 I have already gone through the thread Any difference between await Promise.all() and multiple await?, so I am clear about Promise.all and multiple awaits. Still, I am not very clear about the below 2 scenarios. In Case 1 why does it execute sequentially (takes 10s) whereas in Case 2 it executes in parallel (takes 4s)? Case 1: function promiseWait(time) { return new Promise((resolve, reject) => { setTimeout(() => { resolve(true); }, time); }); } async function test1() { var t0 = performance

Can an ES6 JavaScript promise be resolved by anything else if it is not resolved by the executor?

岁酱吖の 提交于 2021-02-07 11:13:58
问题 If a promise is created this way: myPromise = new Promise(function(resolve, reject) {}); Can this promise be ever resolved by any way? Obviously, the executor is not resolving it in this case, so can the promise be ever resolved? I think if it is jQuery, it can be resolved by deferred.resolve() so that the related promise deferred.promise() is resolved. 回答1: No, the promise can only be resolved or rejected by the resolve and reject functions, respectively. If these functions aren't called nor

Can an ES6 JavaScript promise be resolved by anything else if it is not resolved by the executor?

允我心安 提交于 2021-02-07 11:12:35
问题 If a promise is created this way: myPromise = new Promise(function(resolve, reject) {}); Can this promise be ever resolved by any way? Obviously, the executor is not resolving it in this case, so can the promise be ever resolved? I think if it is jQuery, it can be resolved by deferred.resolve() so that the related promise deferred.promise() is resolved. 回答1: No, the promise can only be resolved or rejected by the resolve and reject functions, respectively. If these functions aren't called nor

Why does this async function execute before the equivalent Promise.then chain defined before it?

人走茶凉 提交于 2021-02-07 04:56:43
问题 I have the following code: var incr = num => new Promise(resolve => { resolve(num + 1); }); var x = incr(3) .then(resp => incr(resp)) .then(resp => console.log(resp)); async function incrTwice(num) { const first = await incr(num); const twice = await incr(first); console.log(twice); } incrTwice(6); Which I believe (perhaps mistakenly) shows two equivalent ways to achieve the same functionality: first by chaining promises and second with the syntactic sugar of async/await. I would expect the

How to make promise.all wait for nested promise.all?

不打扰是莪最后的温柔 提交于 2021-02-07 03:41:11
问题 So I have a problem with JavaScript Promises. I'm using native implementation for the sake of reducing dependencies. An illustrative example of what I need. I need to retrieve lists of books, book authors and purchases. I also need an author profile for each of the authors. After I got all of that, I need to create a nice set of Authors with their books and purchase list for each of the books. Lists and profiles are separate API JSON calls. The only dependency is that I need a list of authors

How to make promise.all wait for nested promise.all?

痞子三分冷 提交于 2021-02-07 03:40:13
问题 So I have a problem with JavaScript Promises. I'm using native implementation for the sake of reducing dependencies. An illustrative example of what I need. I need to retrieve lists of books, book authors and purchases. I also need an author profile for each of the authors. After I got all of that, I need to create a nice set of Authors with their books and purchase list for each of the books. Lists and profiles are separate API JSON calls. The only dependency is that I need a list of authors