es6-promise

How to resolve recursive asynchronous promises?

倖福魔咒の 提交于 2019-12-06 03:33:19
问题 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

Resolving a Promise without calling the 'then'

和自甴很熟 提交于 2019-12-06 02:33:06
I have this code that is part of a small API that I am writing for an NPM module called Poolio. The question I have seems to be a common question for those supporting error-first callbacks as well as promises- how do we support both while maintaining consisent APIs and consistent return values from the API? For example, if I conditionally return a promise from my API, depending on whether the consumer of my lib provides a callback, that is a little awkward in my opinion. The consumer of the lib can provide a callback or use the Promise then function, but not both. Here is a function exported

Conditional .then execution

元气小坏坏 提交于 2019-12-06 01:34:02
问题 How to conditionally skip a promise and do nothing. I have created a nested promise, by which i have 7 .then's. But conditionally, i need to skip few .then and do nothing in that block, how to achive this ? My FULL CODE : const admin = require('firebase-admin'); const rp = require('request-promise'); module.exports = function(req, res) { const phone = String(req.body.phone).replace(/[^\d]/g, ''); const amount = parseInt(req.body.amount); const couponCodeName = (req.body.couponCodeName); const

System.import fails at first attempt (modules[moduleId] is undefined)

北城以北 提交于 2019-12-06 01:09:45
When I use System.import() more than once in separate modules one of them doesn't work on first try (Webpack's require function returns modules[moduleId] is undefined ), but at second and at subsequent attempts it loads normally. Folder structure [webpack-test] ----[modules] --------foo.js --------bar.js --------dateHelper.js ----[node_modules] ----0.app.dist.js ----0.charts.dist.js ----1.app.dist.js ----3.app.dist.js ----app.dist.js ----app.js ----charts.dist.js ----charts.js ----index.html ----package.json ----webpack.config.js index.html <!doctype html> <html lang="en"> <head> <meta charset

Stop other promises when Promise.all() rejects

孤者浪人 提交于 2019-12-05 18:02:40
While all the questions about Promise.all focus on how to wait for all promises , I want to go the other way -- when any of the promises fails, stop the others, or even stop the whole script. Here's a short example to illustrate: const promise1 = new Promise((resolve, reject) => { setTimeout(resolve, 1000, 'resolve1'); }).then(a => { console.log('then1'); return a; }); const promise2 = new Promise((resolve, reject) => { setTimeout(reject, 2000, 'reject2'); }).then(a => { console.log('then2'); return a; }); const promise3 = new Promise((resolve, reject) => { setTimeout(resolve, 3000, 'resolve3'

Updating DOM before blocking code by setTimeout or promise

爷,独闯天下 提交于 2019-12-05 16:56:55
I know that when there is a CPU intensive code any immediate previous DOM update won't happen. Such as function blockFor(dur){ var now = new Date().getTime(); while (new Date().getTime() < now + dur); result.textContent = "I am done..!"; } result.textContent = "Please remain..."; // we will never see this blockFor(2000); <p id="result"></p> However if I shift the CPU intensive code to the asynchronous timeline by setTimeout it's all fine as in the following snippet. function blockFor(dur){ var now = new Date().getTime(); while (new Date().getTime() < now + dur); result.textContent = "I am done

Make an Api call with javascript promises in recursion

∥☆過路亽.° 提交于 2019-12-05 13:06:17
I would like to use the gitter api to get all the messages from a room. What I need to do is to send the get api request with e.g. 50 items, onComplete I need to send another request with 50 items and skip 50 items I already received. Do this request till they won't return any items. So: send api request json parse it : request has items make a sql query with this items proceed the query send next api request (recursion?) ? if no more items in next api request - show done message : request does not have items abort with message I am trying Promises for that, but I got a bit confused with them

Setting a timeout for each promise within a promise.all

久未见 提交于 2019-12-05 11:33:57
I am able to successfully perform a Promise.all, and gracefully handle resolves and rejects. However, some promises complete within a few milliseconds, some can/could take a while. I want to be able to set a timeout for each promise within the Promise.all, so it can attempt to take a maximum of say 5seconds. getData() { var that = this; var tableUrls = ['http://table-one.com','http://table-two.com']; var spoonUrls = ['http://spoon-one.com','http://spoon-two.com']; var tablePromises = that.createPromise(tableUrls); var spoonPromises = that.createPromise(spoonUrls); var responses = {}; var

Unhandled rejection error Bluebird

强颜欢笑 提交于 2019-12-05 10:14:30
I have the following code. And it works as expected without throwing a unhandled rejection error. p = new Promise (fulfill, reject) -> reject new Error 'some error' p.catch (error) -> console.log error Now, the second code example does throw an unhandled rejection error. Can someone explain to me why this is happening when im clearly handling the error. p = new Promise (fulfill, reject) -> reject new Error 'some error' p.then -> console.log 'ok' p.catch (error) -> console.log error Btw. I'm testing in chrome and bluebird v3.4.7 When you chain Promises , each chain is treated as new instance of

using await on global scope without async keyword

我们两清 提交于 2019-12-05 08:59:00
I am trying to do something like this on global scope in nodejs REPL. As per my understanding both the following statements are valid. see docs let x = await Promise.resolve(2); let y = await 2; However, both these statements are throwing an error. Can somebody explain why? my node version is v8.9.4 await can only be used within a function that is labeled async , so there are two ways you can approach this. The first way is to create a self invoked function like this: (async function() { let x = await Promise.resolve(2) let y = await 2 console.log(x, y) })() Or the second way is to use .then()