es6-promise

How do I handle exceptions globally with native promises in node.js?

时光怂恿深爱的人放手 提交于 2019-12-17 07:31:40
问题 I know how to handle specific errors in promises but I sometimes have pieces of code that looks like this: somePromise.then(function(response){ otherAPI(JSON.parse(response)); }); Sometimes, I get invalid JSON which causes a silent failure here when JSON.parse throw s. In general I have to remember to add a .catch handler to every single promise in my code and when I don't I have no way to find out where I forgot one. How do I find these suppressed errors in my code? 回答1: In Modern NodeJS

Promises, pass additional parameters to then chain

半城伤御伤魂 提交于 2019-12-17 07:05:54
问题 A promise, just for example: var P = new Promise(function (resolve, reject) { var a = 5; if (a) { setTimeout(function(){ resolve(a); }, 3000); } else { reject(a); } }); After we call, then the method on the promise: P.then(doWork('text')); doWork function looks like this: function doWork(data) { return function(text) { // sample function to console log consoleToLog(data); consoleToLog(b); } } How can I avoid returning an inner function in doWork, to get access to data from the promise and

Why is the response object from JavaScript fetch API a promise?

痞子三分冷 提交于 2019-12-17 06:08:10
问题 When requesting from a server with JavaScript fetch API, you have to do something like fetch(API) .then(response => response.json()) .catch(err => console.log(err)) Here, response.json() is resolving its promise. The thing is that if you want to catch 404 's errors, you have to resolve the response promise and then reject the fetch promise, because you'll only end in catch if there's been a network error. So the fetch call becomes something like fetch(API) .then(response => response.ok ?

Catch all unhandled javascript promise rejections

白昼怎懂夜的黑 提交于 2019-12-17 05:01:01
问题 I would like to catch all unhandled exceptions/rejections that take place within a javascript Promise. Is there a good method for catching them without adding a .catch(..) on each end of the Promise chain? (in case of forgetting to add this, the error silently disappears). The developer console in Google Chrome can log them, I like to log them as well in a production environment. For normal javascript exceptions I use the window.onerror function, but the errors from a Promise call this

Should I refrain from handling Promise rejection asynchronously?

与世无争的帅哥 提交于 2019-12-17 03:21:46
问题 I have just installed Node v7.2.0 and learned that the following code: var prm = Promise.reject(new Error('fail')); results in this message:; (node:4786) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: fail (node:4786) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. I understand the reasoning behind this as many

ES6 Promises - something like async.each?

狂风中的少年 提交于 2019-12-17 03:14:27
问题 Trying to figure-out how to find something that functional exactly like async.eachSeries, i need a list of async actions run in sequence (not in parallel) but can't find a way to do it in native ES6, can anyone advise, please? p.s. thought about generators/yield but don't have the experience yet so I'm not realized how exactly it can help me. Edit 1 per request, here is an example: Assume this code: let model1 = new MongooseModel({prop1: "a", prop2: "b"}); let model2 = new MongooseModel(

Do I need to return after early resolve/reject?

感情迁移 提交于 2019-12-17 02:54:13
问题 Suppose I have the following code. function divide(numerator, denominator) { return new Promise((resolve, reject) => { if(denominator === 0){ reject("Cannot divide by 0"); return; //superfluous? } resolve(numerator / denominator); }); } If my aim is to use reject to exit early, should I get into the habit of return ing immediately afterward as well? 回答1: The return purpose is to terminate the execution of the function after the rejection, and prevent the execution of the code after it.

Getting API call in node8.10 in Lambda results in Promise <pending> and undefined

混江龙づ霸主 提交于 2019-12-14 03:58:26
问题 I have a Lambda function written in Node8.1 in which I'm trying to get an array of objects (photos of servers from the Unsplash API), then write them to DynamoDB. Right now I can't get the results of my API call, despite chaining promises. Any help would be greatly appreciated. I've tried chaining promises as well as async/await in my function, but keep getting the following errors: Promise { <pending>, ... and TypeError: Cannot read property 'results' of undefined at unsplash.search.photos

What determines the call order of deferred function using promises or setTimeout?

最后都变了- 提交于 2019-12-14 03:43:55
问题 Deferring the execution of functions, for example in custom event handling, is a common pattern in JavaScript (see, for example here). It used to be that using setTimeout(myFunc,0) was the only way to do this, however with promises there is now an alternative: Promise.resolve().then(myFunc) . I had assumed that these would pretty much do the same thing, but while working on a library which included custom events I thought I'd find out if there was a difference, so I dropped the following

Applying update query on a record of 100,000 users on cassandra table using nodejs. Busy Connection issue

牧云@^-^@ 提交于 2019-12-14 02:27:30
问题 I'm working with cassandra 3.x and node 10.13.0. I've data of 100,000 users in my working memory (in a map 'sortedRowMap' as in given code). I'm updating all of the record by iterating the map (having record of 100,000) users using for each. But it's throwing me BusyConnectionError. I'm wondering how could I get out from this. Below is the implemented code of the aforementioned description. var cassClient = new cassandra.Client({contactPoints: ['localhost'],pooling: { coreConnectionsPerHost: