es6-promise

Keeping Promise Chains Readable

て烟熏妆下的殇ゞ 提交于 2019-12-02 07:16:10
问题 I've grown used to promise chaining arrays. It's incredibly easy to read a promise chain when each promise is a line long such as myArray.map(x => convertX) .filter() .whatever() .etc() This is incredibly easy to read. However, when I create promise chains with custom functions, it gets much messier. database.query(first query) .then(results => { // do stuff // do more // even more return database.query(second query) }) .then(results => { // rinse and repeat }) .catch(err => { // error

Keeping Promise Chains Readable

拜拜、爱过 提交于 2019-12-02 06:54:03
I've grown used to promise chaining arrays. It's incredibly easy to read a promise chain when each promise is a line long such as myArray.map(x => convertX) .filter() .whatever() .etc() This is incredibly easy to read. However, when I create promise chains with custom functions, it gets much messier. database.query(first query) .then(results => { // do stuff // do more // even more return database.query(second query) }) .then(results => { // rinse and repeat }) .catch(err => { // error handling }) Now, this can be legible, but when the promise chain extends further, it gets to be a little much

Is there a way detect if a rejected promise is unhandled?

霸气de小男生 提交于 2019-12-02 06:34:24
Let’s say I have a function foo which returns a promise. Is there a way to call the function, and optionally Promise.prototype.catch the result only if the rejection is unhandled? I want a solution which works in both node.js and the browser. For example: const fooResult = foo(); // pass fooResult somewhere else where fooResult may be caught with catch catchIfUncaught(fooResult, (err) => { console.log(err); // should be foo rejection only if the rejection is not caught elsewhere // no unhandled rejection occurs }); No, there is not. When your function returns a promise, that leaves error

How to simplify deep nested promises

浪尽此生 提交于 2019-12-02 05:24:27
问题 I have come across a situation where I need to break at every "else" clause in a then() , and this doesn't look any better than nested callbacks, this is a login process: User.findOne({ username: username }).exec() .then(user => { if (user) { return user.verifyPassAsync() .then(matched => { if (matched) { return User.getBriefProfile(username)) .then(emp => { if (emp) { return saveToSession(emp); } else { //return }}) } else { //return ... }}) } else { // return false } }) Is there any way I

Fetch api - getting json body in both then and catch blocks for separate status codes

落花浮王杯 提交于 2019-12-02 05:17:25
I am using fetch api for fetching an URL that might return: Response : status = 200, json body = {'user': 'abc', 'id': 1} or Response : status = 400 , json body = {'reason': 'some reason'} or Response : status = 400 , json body = {'reason': 'some other reason'} I want to make a separate function request() that I use from various parts of my code as follows: request('http://api.example.com/').then( // status 200 comes here data => // do something with data.id, data.user ).catch( // status 400, 500 comes here error => // here error.reason will give me further info, i also want to know whether

How to chain promises in for loop in vanilla javascript

喜欢而已 提交于 2019-12-02 04:03:32
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: 'picture,album'}, function(response) { arr.push(response); } })(x); } Assuming your ajax calls can

Receiving `UnhandledPromiseRejectionWarning` even though rejected promise is handled

拈花ヽ惹草 提交于 2019-12-02 02:03:15
问题 I have constructed a function which iterates through a Generator containing both synchronous code and Promises : module.exports = { isPromise (value) { return typeof value === 'object' && value !== null && 'then' in value; }, runGen (generatorFunc, startValue) { let that = this, i = 0; function *iterator(resolve, reject) { let runGeneratorFunc = generatorFunc(startValue), yieldedOut = {done: false}, yieldIn; while (!yieldedOut.done) { console.log(i++, 'Ready for next iteration'); if (that

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

我们两清 提交于 2019-12-02 01:30:52
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 read property 'then' of undefined on my local machine with node.js installed and can't figure out why.

Why is synchronous sleep function not made async by being inside promise?

梦想的初衷 提交于 2019-12-02 01:06:44
问题 I'm trying to wrap my head around promises, and how JavaScript works with it's queue and event loop etc. I thought that if I put a slow synchronous function inside a promise, that slow sync function would be delegated to the background and I could use .then to deal with it when it was done. function syncSleep(ms){ var end = new Date().getTime() + ms; var start = new Date().getTime(); while (start < end) { start = new Date().getTime(); } } function p() { return new Promise(function(resolve) {

Receiving `UnhandledPromiseRejectionWarning` even though rejected promise is handled

两盒软妹~` 提交于 2019-12-02 00:54:47
I have constructed a function which iterates through a Generator containing both synchronous code and Promises : module.exports = { isPromise (value) { return typeof value === 'object' && value !== null && 'then' in value; }, runGen (generatorFunc, startValue) { let that = this, i = 0; function *iterator(resolve, reject) { let runGeneratorFunc = generatorFunc(startValue), yieldedOut = {done: false}, yieldIn; while (!yieldedOut.done) { console.log(i++, 'Ready for next iteration'); if (that.isPromise(yieldedOut.value)) { console.log(i++, 'Pass promise to KeepIterating'); yieldIn = yield