es6-promise

How do you implement a “raceToSuccess” helper, given a list of promises?

☆樱花仙子☆ 提交于 2019-12-02 22:42:27
I'm puzzled by something in the ES6 Promise API. I can see a clear use case for submitting multiple async jobs concurrently, and "resolving" on the first success. This would, for example, serve a situation where multiple equivalent servers are available, but some are perhaps down, and others heavily loaded and slow, so my goal would be to get a response from the first one to succeed, and ignore the rest (yes, I know this is an obnoxious way for a client to behave from a server's perspective, but it's great for the end user ;) However, as far as I can see, I have either "all" or "race"

multiple promises running in parallel, $q.all needs chaining?

我与影子孤独终老i 提交于 2019-12-02 22:15:03
问题 I have 3 parallel promises or api requests once all teh three are done, I need to call another api request based on the second promise and then finally call .then( of $q.all Here is the code getAllLocations() { //make a promise call for all here . var promise = []; ̶p̶r̶o̶m̶i̶s̶e̶.̶p̶u̶s̶h̶(̶t̶h̶i̶s̶.̶g̶e̶t̶A̶l̶l̶L̶o̶c̶a̶t̶i̶o̶n̶s̶(̶I̶d̶)̶.̶t̶h̶e̶n̶(̶ promise.push(this.getLocations(Id).then( (locationsData) => { this.locations = locationsData; })); promise.push(this.getAllStates(Id).then(

What is the best general practice to timeout a function in promise [closed]

心已入冬 提交于 2019-12-02 21:59:33
Promisify a function call with timeouts I have seen many resources provide similar examples of using Promise.race to timeout a function call within a given period of time. This is a very good example of how Promise.race can be used in practice. Here's some sample code: function doWithinInterval(func, timeout) { var promiseTimeout = new Promise(function (fulfill, reject) { // Rejects as soon as the timeout kicks in setTimeout(reject, timeout); }); var promiseFunc = new Promise(function (fulfill, reject) { var result = func(); // Function that may take long to finish // Fulfills when the given

Mocha exceeding 2000ms timeout when returning a promise [duplicate]

无人久伴 提交于 2019-12-02 17:54:16
问题 This question already has answers here : Change default timeout for mocha (4 answers) Closed 2 years ago . I'm trying to test my Express.js controllers but every so often I run into a problem with the mocha timeout telling me off. The docs (https://mochajs.org/#working-with-promises) and the answer here: https://stackoverflow.com/a/26572442/1646372 state that I can just return the promise I'm using. I've wrapped my express controllers with Promises so that I can then return them in the tests.

Recursion call async func with promises gets Possible Unhandled Promise Rejection

浪子不回头ぞ 提交于 2019-12-02 15:58:47
问题 const PAGESIZE = 1000; const DEFAULTLINK = `${URL}/stuff?pageSize=${PAGESIZE}&apiKey=${APIKEY}`; export const getAllStuff = (initialLink = DEFAULTLINK) => { let allStuff = {}; return getSuffPage(initialLink) .then(stuff => { allStuff = stuff; if (stuff.next) { return getAllStuff(stuff.next) .then(nextStuff => { allStuff = Object.assign({}, stuff, nextStuff); return allStuff; }); } else { return allStuff; } }); }; const getSuffPage = nextPageLink => { fetch(nextPageLink).then(res => { return

What is the point of promises in JavaScript?

情到浓时终转凉″ 提交于 2019-12-02 14:53:38
A promise is a (...) value which may be available now, or in the future, or never (Source: MDN) So lets say I have an app which wants to work with pictures. The pictures are loaded e.g. after an algorithm works with it in the background (or some other sort of delay). Now I want to check, if the pictures are available in the future , by using a promise, not a callback. To check, if an image is available, I could use the following code: function loadImage(url) { return new Promise((resolve, reject) => { let image = new Image() image.onload = function() { resolve(image) } image.onerror = function

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

流过昼夜 提交于 2019-12-02 13:37:13
问题 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,

multiple promises running in parallel, $q.all needs chaining?

◇◆丶佛笑我妖孽 提交于 2019-12-02 08:25:33
I have 3 parallel promises or api requests once all teh three are done, I need to call another api request based on the second promise and then finally call .then( of $q.all Here is the code getAllLocations() { //make a promise call for all here . var promise = []; ̶p̶r̶o̶m̶i̶s̶e̶.̶p̶u̶s̶h̶(̶t̶h̶i̶s̶.̶g̶e̶t̶A̶l̶l̶L̶o̶c̶a̶t̶i̶o̶n̶s̶(̶I̶d̶)̶.̶t̶h̶e̶n̶(̶ promise.push(this.getLocations(Id).then( (locationsData) => { this.locations = locationsData; })); promise.push(this.getAllStates(Id).then( (resp) => { this.states = resp.data; })); promise.push(this.getTerritories(Id).then( (resp) => { this

Recursion call async func with promises gets Possible Unhandled Promise Rejection

那年仲夏 提交于 2019-12-02 08:02:38
const PAGESIZE = 1000; const DEFAULTLINK = `${URL}/stuff?pageSize=${PAGESIZE}&apiKey=${APIKEY}`; export const getAllStuff = (initialLink = DEFAULTLINK) => { let allStuff = {}; return getSuffPage(initialLink) .then(stuff => { allStuff = stuff; if (stuff.next) { return getAllStuff(stuff.next) .then(nextStuff => { allStuff = Object.assign({}, stuff, nextStuff); return allStuff; }); } else { return allStuff; } }); }; const getSuffPage = nextPageLink => { fetch(nextPageLink).then(res => { return res.json(); }); }; Calling getAllStuff throws: Possible Unhandled Promise Rejection (id: 0): TypeError:

Avoiding callback hell using generators and promises (Co module)

断了今生、忘了曾经 提交于 2019-12-02 07:32:38
I am new to nodejs. Here, I want to avoid callback using Co generators with promises. But when I execute this code, it only executes first yield or does not assign the result of get function to processedData0 variable. How to solve this ? co(function *() { console.log("hello"); try{ console.log("hello"); var processedData0= yield Promise.promisify(get); //console.log("hello"); var processedData1 = yield Promise.promisify(abc1)(processedData0); //console.log("hello2"); var processedData2 = yield Promise.promisify(xyz)(processedData1); //console.log("hello3"); var processedData3 = yield Promise