es6-promise

chain array of promises with bluebird

三世轮回 提交于 2019-12-10 14:29:49
问题 I'm a working my way with promises and I'm stuck with my use case. I have an array of transformer functions (each function is a promise and modifies some JSON structure). Let me show some code. Lets say this is my JSON structure (array) var data = [{a: 1, b:2}, {a:3, b:4}]; transformFunction is definition of transform functions modifying the data a certain way. The two functions adds c and d property to the above JSON structure: var transformFunctions = { // transform1: function (data) { //

How promises and promise chaining works? [Problem with code]

半城伤御伤魂 提交于 2019-12-10 12:58:07
问题 I'm learning Node.js and trying to properly use the mysql2 module. Because of this, I have recently started researching about promises. I'm writing a kind of "library", so I can practice all these topics, and while doing that, I got into a problem with promise chaining I can't really understand. Any help is well appreciated! The problem is the following: Let's say that I have a query function which fetches the database, process the data and returns a promise, so I can get that data and work

Mocha: Error Timeout of 2000ms exceeded

五迷三道 提交于 2019-12-10 12:37:22
问题 I am trying to seed the database for unit test. Below is the seed.js file: ....... const app = require('./app') const db = app.get('db') const saveUsersToDB = (done) => { db.User.bulkCreate(users) .then(() => (done)) } module.exports = {saveUsersToDB}; My app.test.js file: ....... const expect = require('expect') const request = require('supertest') const {saveUsersToDB} = require('./seed/seed'); before(saveUsersToDB) When I run the test below is the error I get: Express listening on port

Catch Promise rejection at a later time [duplicate]

落爺英雄遲暮 提交于 2019-12-10 12:34:06
问题 This question already has answers here : Waiting for more than one concurrent await operation (4 answers) Closed 12 months ago . How do I retrieve the result of a promise at a later time? In a test, I am retrieving an email before sending further requests: const email = await get_email(); assert.equal(email.subject, 'foobar'); await send_request1(); await send_request2(); How can I send the requests while the slow email retrieval is going on? At first, I considered awaiting the email later: /

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

好久不见. 提交于 2019-12-10 10:17:29
问题 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

Cannot invoke an expression whose type lacks a call signature for function returning resolved promise

做~自己de王妃 提交于 2019-12-10 05:56:37
问题 I'm trying to call a function which returns a resolved promise using Promise.resolve based on some condition. An over simplified version of the function is as follows: function fullFilledPromiseReturner(num: number) { if (num > 5) { return Promise.resolve(5); } else { return Promise.resolve(); } } fullFilledPromiseReturner(4).then(data => { console.log(data); }); Now TypeScript is not letting it go through compiler and is throwing following error: [ts] Cannot invoke an expression whose type

What happens when a promise is resolved multiple times

百般思念 提交于 2019-12-10 04:46:15
问题 What is the standard behaviour if a ES6 promise is rejected / resolved multiple times? The following code was only resolved once in Google Chrome , is this the standard behaviour in all browsers? new Promise(function(e) { $('#button').click(function(){ resolve(); }); }); I have seen a promise polyfill throwing an exception on trying to resolve an already resolved promise. Does the specification for es6-promise specify this, or isn't the polyfill standard compliant? Update Sorry, I just

Does JavaScript promise create memory leaks when not rejected or resolved?

前提是你 提交于 2019-12-10 04:08:24
问题 I'm in a situation where I need execute async functions in "parallel", and continue program execution with the best result. Thus I wrote something like this : var p = []; for (var i = 0; i < 10; ++i) (function (index) { p.push(new Promise(function (resolve, reject) { setTimeout(function () { var success = Math.random() > 0.7; console.log("Resolving", index, "as", success ? "success" : "failure"); success && resolve(index); }, Math.random() * 5000 + 200); })); })(i); Promise.race(p).then

Javascript Recursive Promise

久未见 提交于 2019-12-10 03:21:17
问题 I'm trying to create a recursive function using Promises, but can't quite seem to get it right. I have working code without using promises, but it uses counters and global variables etc. and doesn't feel quite right, so I'm attempting a rewrite and also creating a module for reuse. Essentially, the function is supposed to be getting a user from Active Directory and then recursively finding any direct reports and their direct reports and so on. I've played with lots of versions of the function