es6-promise

Understanding promise.race() usage

若如初见. 提交于 2019-11-29 06:44:59
As far as I know, there are two options about promise : promise.all() promise.race() Ok, I know what promise.all() does. It runs promises in parallel, and .then gives you the values if both resolved successfully. Here is an example: Promise.all([ $.ajax({ url: 'test1.php' }), $.ajax({ url: 'test2.php' }) ]) .then(([res1, res2]) => { // Both requests resolved }) .catch(error => { // Something went wrong }); But I don't understand what does promise.race() is supposed to do exactly? In other word, what's the difference with not using it? Assume this: $.ajax({ url: 'test1.php', async: true,

Best es6 way to get name based results with Promise.all

别说谁变了你拦得住时间么 提交于 2019-11-29 05:44:42
By default the Promise.All([]) function returns a number based index array that contains the results of each promise. var promises = []; promises.push(myFuncAsync1()); //returns 1 promises.push(myFuncAsync1()); //returns 2 Promise.all(promises).then((results)=>{ //results = [0,1] } What is the best vanilla way to return a named index of results with Promise.all()? I tried with a Map, but it returns results in an array this way: [key1, value1, key2, value2] UPDATE: My questions seems unclear, here is why i don't like ordered based index: it's crappy to maintain : if you add a promise in your

How to check if a Promise is pending [duplicate]

微笑、不失礼 提交于 2019-11-29 02:09:26
问题 This question already has an answer here: How can I synchronously determine a JavaScript Promise's state? 20 answers I have this situation in which I would like to know what the status is of a promise. Below, the function start only calls someTest if it is not running anymore (Promise is not pending). The start function can be called many times, but if its called while the tests are still running, its not going to wait and returns just false class RunTest { start() { retVal = false; if (!this

how to cancel/abort ajax request in axios

北战南征 提交于 2019-11-29 01:55:46
问题 I use axios for ajax requests and reactJS + flux for render UI. In my app there is third side timeline (reactJS component). Timeline can be managed by mouse's scroll. App sends ajax request for the actual data after any scroll event. Problem that processing of request at server can be more slow than next scroll event. In this case app can have several (2-3 usually) requests that already is deprecated because user scrolls further. it is a problem because every time at receiving of new data

Get Bluebird Promise from async await functions

我们两清 提交于 2019-11-29 01:52:24
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 getResolvedAsyncAwaitPromise = async () => 'value'; getResolvedAsyncAwaitPromise() .tap(...) // Error ! Native Promises does

Delays between promises in promise chain

喜夏-厌秋 提交于 2019-11-28 23:38:30
Let's say I am using the following code to run a couple of promises in series: let paramerterArr = ['a','b','c','d','e','f'] parameterArr.reduce(function(promise, item) { return promise.then(function(result) { return mySpecialFunction(item); }) }, Promise.resolve()) The code simply calls mySpecialFunction (which returns a promise), waits for the promise to be resolved and then calls mySpecialFunction again etc. So the function is called once for every element in the array, in the correct order. How could I make sure that there is a delay of at least 50 milliseconds between every call of

Make server validation using redux-form and Fetch API

与世无争的帅哥 提交于 2019-11-28 22:58:08
问题 How to make server-side validation using redux-form and Fetch API? There are "Submit Validation" demo provided in the docs which says that recommended way to do server side validation is to return a promise from the onSubmit function. But where should I place that promise? As I understood onSubmit function should be my action. <form onSubmit={this.props.addWidget}>... Where this.props.addWidget is actually my action, provided below. import fetch from 'isomorphic-fetch'; ... function

ES6 Promise.all progress

余生长醉 提交于 2019-11-28 21:30:32
I have several promises that I need to resolve before going further. Promise.all(promises).then((results) => { // going further }); Is there any way I can have the progress of the Promise.all promise? From the doc, it appears that it is not possible . And this question doesn't answer it either. So: Don't you agree that this would be useful? Shouldn't we query for this feature? How can one implement it manually for now? I've knocked up a little helper function that you can re-use. Basically pass your promises as normal, and provide a callback to do what you want with the progress.. function

Is there a version of setTimeout that returns an ES6 promise?

六眼飞鱼酱① 提交于 2019-11-28 21:06:12
Similar to this question , but rather than asking about how promises work in general, I specifically want to know: What is the standard/best way to wrap setTimeout in something that returns a Promise ? I'm thinking something like Angular's $timeout function , but not Angular specific. In Browsers First of all no - there is no built in for this. Lots of libraries that enhance ES2015 promises like bluebird whip with it. I think the other answer conflates executing the function and a delay, it also creates timeouts that are impossible to cancel. I'd write it simply as: function delay(ms){ var ctr

Node.js: When to use Promises vs Callbacks

不想你离开。 提交于 2019-11-28 19:35:06
I have some older Node.js code that I'm updating. In the process I'm designing new modules to work with the old code. I'm finding that now, as opposed to when I first wrote this, I rely more on using ES6 promises rather than callbacks. So now I have this mix of some functions returning promises and some taking callbacks - which is tedious. I think eventually it should be refactored to use promises. But before that is done... What are the situations where promises are preferred and where are callbacks preferred? Is there any type of situation that a callback can handle better than a promise and