es6-promise

What is faster: try catch vs Promise

99封情书 提交于 2019-12-29 05:30:23
问题 I heard such an opinion that you should avoid usage of try/catch at all because it takes many resources. So could promise error handling could be faster? Or it does not matter at all? function f(somethingDangerous) { return new Promise((resolve, reject) => { // try { // somethingDangerous(); // resolve(); // } catch (err) { // reject(err); // } // VS somethingDangerous(); resolve(); }).catch((err) => { console.error('Catched: ' + err); }); } f(() => {throw 'DANGEROUS THING';}); UPD : I know

How to run after all javascript ES6 Promises are resolved

主宰稳场 提交于 2019-12-29 00:06:09
问题 I'm in the process of replacing some old code that used jQuery Deferred objects and I am rewriting using Bluebird/ES6 Promises. If I have multiple asynchronous calls, how can I trigger a function after all the promises are resolved. Using jQuery Deferreds it would be something like this: var requests = [...]; //some arbitrary data that is iterated to generate multiple ajax requests var promises = []; resuests.forEach(function(endpoint) { promises.push($.ajax({url: endpoint})); }); $.when

NodeJS, Promises and performance

为君一笑 提交于 2019-12-28 06:53:32
问题 My question is about performance in my NodeJS app... If my program run 12 iteration of 1.250.000 each = 15.000.000 iterations all together - it takes dedicated servers at Amazon the following time to process: r3.large: 2 vCPU, 6.5 ECU, 15 GB memory --> 123 minutes 4.8xlarge: 36 vCPU, 132 ECU, 60 GB memory --> 102 minutes I have some code similair to the code below... start(); start(){ for(var i=0; i<12; i++){ function2(); // Iterates over a collection - which contains data split up in

Get first fulfilled promise

徘徊边缘 提交于 2019-12-28 06:35:27
问题 If I have two promises A and B, only one of which will succeed, how can I get whichever one fulfills successfully? I'm looking for something similar to Promise.race , but which will return only the first promise that fulfills. I'm using promises from ES6. 回答1: Invert the polarity of the promises, and then you can use Promise.all , because it rejects on the first rejected promise, which after inversion corresponds to the first fulfilled promise: const invert = p => new Promise((res, rej) => p

Get Bluebird Promise from async await functions

狂风中的少年 提交于 2019-12-28 06:01:14
问题 I am looking for a way, with Node v7.6 or above, to get a Bluebird Promise (or any non-native promise) when an async function is called. In the same way I can do: global.Promise = require('Bluebird'); // Or Q/When var getResolvedPromise = () => Promise.resolve('value'); getResolvedPromise .tap(...) // Bluebird method .then(...); See: May I use global.Promise=require("bluebird") I want to be able to do something like: global.Promise = require('Bluebird'); // Or Q/When var

What will happen if I set the request's mode to 'no-cors' in my firebase cloud function?

时光怂恿深爱的人放手 提交于 2019-12-25 17:21:15
问题 This is a follow up to this question. I have a firebase function which is supposed to take an OTP, validate it and then change a user's password based on whether it is correct or not (for some reason I'm not able to use firebase's inbuild password reset functionality). Following is my function: exports.resetPassword = functions.https.onCall((data, context) => { return new Promise((resolve, reject) => { if(data.sesId && data.otp){ admin.firestore().collection('verification').doc(data.sesId)

What will happen if I set the request's mode to 'no-cors' in my firebase cloud function?

為{幸葍}努か 提交于 2019-12-25 17:21:09
问题 This is a follow up to this question. I have a firebase function which is supposed to take an OTP, validate it and then change a user's password based on whether it is correct or not (for some reason I'm not able to use firebase's inbuild password reset functionality). Following is my function: exports.resetPassword = functions.https.onCall((data, context) => { return new Promise((resolve, reject) => { if(data.sesId && data.otp){ admin.firestore().collection('verification').doc(data.sesId)

Promise.resolve inside then() method does not pass its resolved value

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-25 09:28:35
问题 I found an interest thing about Promise. When I run the following codes, it gave me the output of 'aa' as oppose to 'bb', which confused me a lot. Does anyone understand why and give a explanation, please? Thanks! Promise.resolve('aa') .then(Promise.resolve('bb')) .then(console.log); 回答1: Well, you're misusing a .then() handler so it is no surprise that you don't get the desired answer. A .then() handler should be passed a function reference. You are passing it a promise which it dutifully

How to pass arguments from the previous then statement to the next one

谁都会走 提交于 2019-12-25 09:26:11
问题 I am having fun with Promises and have one problem. Here is my code req.checkBody(BookManager.SCHEME); req.getValidationResult() .then(function (result) { if (!result.isEmpty()) { handler.onError('Invalid payload'); return; } return new BookModel({ author: data.author, name: data.name, year: data.year }); }) .then((book) => { book.save(); }) .then((saved) => { handler.onSuccess(saved); }) .catch((error) => { handler.onError(error.message); }); But for some reason this then statement .then(

Why is my apolloFetch call returning an empty query when called from within a promise.all?

风流意气都作罢 提交于 2019-12-25 03:12:46
问题 I'm trying to use apolloFetch inside a Promise.all in my Node.js microservice but keep getting an error that the query is empty. The reason for using apolloFetch is to call another micro service and pass it an array of queries. Can someone give me some direction? My code is as follows: const uri = "dsc.xxx.yyyy.com/abc/def/graphql"; const apolloFetch = CreateApolloFetch({uri}); const QryAllBooks = { type: new GraphQLList(BookType), args: {}, resolve() { return new Promise((resolve, reject) =>