es6-promise

How to chain a Promise.all with other Promises?

只谈情不闲聊 提交于 2019-12-04 09:55:43
问题 I want to execute my code in the following order: Promise 1 Wait for 1 to be done, then do Promise 2+3 at the same time Final function waits for Promise 2+3 to be done I'm having some trouble figuring it out, my code so far is below. function getPromise1() { return new Promise((resolve, reject) => { // do something async resolve('myResult'); }); } function getPromise2() { return new Promise((resolve, reject) => { // do something async resolve('myResult'); }); } function getPromise3() { return

How to resolve recursive asynchronous promises?

本秂侑毒 提交于 2019-12-04 08:51:22
I'm playing around with promises and I'm having trouble with an asynchronous recursive promise. The scenario is an athlete starts running the 100m, I need to periodically check to see if they have finished and once they have finished, print their time. Edit to clarify : In the real world the athlete is running on a server. startRunning involves making an ajax call to the server. checkIsFinished also involves making an ajax call to the server. The code below is an attempt to imitate that. The times and distances in the code are hardcoded in an attempt to keep things as simple as possible.

TypeScript type definition for promise.prototype.finally

落花浮王杯 提交于 2019-12-04 06:58:58
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 represent this in TypeScript. Any ideas? Possibly related: https://github.com/Microsoft/TypeScript

Why does JS promise print all the resolve first then rejects second

不羁的心 提交于 2019-12-04 06:50:10
Why does promise print all the success first then the rejects after, even though i wrote the code for it to appear randomly var errors = 0; var successes = 0; var p1; for (var i = 0; i < 10; i++) { p1 = new Promise(function(resolve, reject) { var num = Math.random(); if (num < .5) { resolve(num); } else { reject(num) } }); p1.then(function success(res) { successes++; console.log("*Success:* " + res) }).catch(function error(error) { errors++ console.log("*Error:* " + error) }); } OUTPUT VM331:16 *Success:* 0.28122982053146894 VM331:16 *Success:* 0.30950619874924445 VM331:16 *Success:* 0

Correct pattern for multiway flows with Promises

笑着哭i 提交于 2019-12-04 05:35:29
So i have been playing with promises for the last few days, and just trying to convert some project, to use promises, but more than a few times i have encuntered this issue. While reading articles and tutorials everything looks smooth and clean: getDataFromDB() .then(makeCalculatons) .then(getDataFromDB) .then(serveToClient) But in reality, its not like that. Programs have a lot of "if conditions" that changes the whole flow: getDataFromCache(data).then(function(result){ if(result){ return result; }else{ return getDataFromDB(); } }).then(function(result){ if(result){ serveToClient() //this

Is it possible to wrap promise inside generator?

我怕爱的太早我们不能终老 提交于 2019-12-04 05:11:23
I'm trying to create a promise-wrapper using generator so that I can do: var asyncResult = PromiseWrapper( $.ajax( ... ) ); So far, I've been trying with: function PromiseWrapper(promise){ return function *wrapper(promise){ promise.then(function(result){ yield result; }, function(err){ throw err; }); }(promise).next().value } but this fails because yielding inside a normal is not allowed. Is there any work-around for this? Thank you :D ps: I'm using babel to translate the code from es6 to es5 Bergi It is utterly impossible to wrap a promise in a generator that synchronously yields the promise

Value of Promise.all() after it is rejected, shows [''PromiseStatus'']: resolved if catch block is present

馋奶兔 提交于 2019-12-04 03:28:42
问题 I have two promises, one rejected and other resolved. Promise.all is called. It executed the catch block of Promise.all as one of the promises is rejected. const promise1 = Promise.resolve('Promise 1 Resolved'); const promise2 = Promise.reject('Promise 2 Rejected'); const promise3 = Promise.all([promise1, promise2]) .then(data => { console.log('Promise.all Resolved', data); }) .catch(error => { console.log('Promise.all REJECTED', error); }) setTimeout(() => { console.log(promise1, promise2,

Promise.resolve with no argument passed in

我怕爱的太早我们不能终老 提交于 2019-12-04 02:33:32
In the OpenUI5 code-base I came across this snippet: // Wait until everything is rendered (parent height!) before reading/updating sizes. // Use a promise to make sure // to be executed before timeouts may be executed. Promise.resolve().then(this._updateTableSizes.bind(this, true)); It looks like the native Promise function is being used, with no argument being passed to it's resolve function which takes an: Argument to be resolved by this Promise. Can also be a Promise or a thenable to resolve. So, since it looks like the promise would simply immediately resolve and invoke then 's callback,

ES5 vs ES6 Promises

旧城冷巷雨未停 提交于 2019-12-03 22:57:21
I wanna know whether JS promises were a part of es5? If so, why it doesn't work sometimes in older browsers and we have to add a polyfill for the same. Also, which polyfill should be added in that case, an es5 one or es6? I have a little confusion regarding the same. jfriend00 ES5 did not have promises. Libraries like jQuery or Angular had their own custom and non-standard promise implementations. Popular Promise implementations for use with ES5 are Bluebird (which is compatible with the ES6 standard) and Q (which was not originally compatible with the ES6 standard- though seems to be moving

chaining async method calls - javascript

℡╲_俬逩灬. 提交于 2019-12-03 18:07:44
问题 You have a prototype object Foo with two async method calls, bar and baz. var bob = new Foo() Foo.prototype.bar = function land(callback) { setTimeout(function() { callback() console.log('bar'); }, 3000); }; Foo.prototype.baz = function land(callback) { setTimeout(function() { callback() console.log('baz'); }, 3000); }; We want to do bob.bar().baz() and have it log "bar" and "baz" sequentially. If you cannot modify the method calls (including passing in your callback function), how can you