es6-promise

How do I catch ES6 Promise rejections and completely stop flow?

一世执手 提交于 2020-01-01 12:07:51
问题 Say I have 4 functions: runA() , runB() , runC() and runD() . Using ES6 promises, in a completely successful run, these would all be run one after another: runA() .then(runB) .then(runC) .then(runD) If runA or runB fail (reject or throw), I would like to call error1() and then completely stop the chain (not call runC or runD ). This makes me think I should add a single .catch() at the very end of the .then promise chain: runA() .then(runB) .then(runC) //won't get called if runA or runB throws

TypeScript type definition for promise.prototype.finally

允我心安 提交于 2020-01-01 12:03:07
问题 I was using this ES6 Promise compatible finally implementation called promise.prototype.finally in a Node application that I want to convert to TypeScript however there are no typing available for this package that I can find on DefinitelyTyped. In these situations I've written up my own impromptu type definitions for just the subset of functionality that I need but in this case it is a library that modifies the prototype of the Promise object and I haven't encountered any conventional way to

Async Mongoose callbacks in then statement

可紊 提交于 2019-12-31 06:57:07
问题 I have the issue with using promises and async calls from Mongoose 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) => { BookModel.find({name: book.name}, function (err, docs) { if (docs.length) { throw new Error("Book already exists"); } else { return book; } }); }) .then(

How to chain promises in for loop in vanilla javascript

拜拜、爱过 提交于 2019-12-31 04:21:13
问题 If I am doing an async call like the following, how can chain them with promises, so i can do stuff in order? In this example, what ends up happening is that arr will push the items out of order. I'd prefer an answer with promises, but anything will do as long as it works var fbArrOfAlbumNames = ['Profile Pictures', 'Cover Photos', 'Mobile Uploads']; var arr = []; for(var x = 0; x < fbArrOfAlbumNames.length; x++) { (function(cntr) { FB.api('/' + fbArrOfAlbumNames[cntr] + '/photos/', {fields:

How to properly check and log http status code using promises and node.js?

谁说我不能喝 提交于 2019-12-31 02:15:09
问题 I am new to JavaScript and very new to node.js framework, just started using it a few days ago. My apologies if my code is nonsensical, the whole idea of promises and callbacks is still sinking in. That being said my question is the following I am trying to figure out if certain request to websites are successful or cause an error based on the range of their status code response. I am working with an array of websites and what I've done so far is below, I do however get a TypeError: Cannot

Cleanest way to handle custom errors with fetch & ES6 promise

梦想与她 提交于 2019-12-30 03:18:08
问题 I am trying to intelligently handle the success/error responses from our API using fetch & ES6 promises. Here is how I need to handle response statuses: 204: has no json response, but need to treat as success 406: should redirect to sign in 422: has json for error message < 400 (but not 204): success, will have json >= 400 (but not 422): error, will not have json So, I am struggling with how to write this cleanly. I have some less than stellar code working right now that looks like this:

How to extract data out of a Promise

随声附和 提交于 2019-12-30 01:59:09
问题 I have a promise that returns data and I want to save that in variables. Is this impossible in JavaScript because of the async nature and do I need to use onResolve as a callback? Can I somehow use this (e.g. wrap it with async/await): const { foo, bar } = Promise.then(result => result.data, errorHandler); // rest of script instead of this? Promise.then(result => { const { foo, bar } = result.data; // rest of script }, errorHandler); Note: Bluebird library is used instead of native

promise resolve before inner promise resolved

好久不见. 提交于 2019-12-30 00:39:08
问题 I have a promise and I want it to resolve only when inner promise has resolved. Right now it resolves before the "resolve" function has been reached in the "loadend" callback. What am I missing? I am confused about the way you are supposed to use resolve and about how you can use a promise within another promise. I couldn't find anything that helped on the web. In the following example I basically load a bunch of files, for each file I get a blob and I want to pass this blob in a file reader.

Do I always need catch() at the end even if I use a reject callback in all then-ables?

前提是你 提交于 2019-12-29 09:58:21
问题 I am putting catches at the end, but they are returning empty object in one particular instance at least. Is a catch necessary for anything unbeknownst, or is it just screwing me up? $( document).ready(function(){ app.callAPI()//a chainable a RSVP wrapper around a jquery call, with its own success() fail() passing forward to the wrapper, so it will either be a resolved or rejected thenable to which is now going to be chained .then( function(env) { //set the property you needed now app

Uncaught (in promise) Error

这一生的挚爱 提交于 2019-12-29 08:10:14
问题 This is obviously a SSCCE. I have the following (jsFiddle here): <html> <body> <input id='file-dlg' type='file'/> <br/> <button id='submit' type='button'>submit</button> <script> document.getElementById('file-dlg').addEventListener('change', storeAPromise); var p; function storeAPromise() { p = new Promise(function executor(resolve, reject) { try { throw new Error('snafu'); } catch(e) { reject(e); } }); }; document.getElementById('submit').onclick = function() { p.then(function() {}, function