es6-promise

sinon stub with es6-promisified object

Deadly 提交于 2019-12-11 11:43:39
问题 Ok my setup is as follows: Using node 6.2, es6-promisify, sinon, sinon-as-promised, and babel to transpile support for es6 import/export. My code under test looks something like this: const client = restify.createJsonClient({ url: 'http://www.example.com' }); export let get = promisify(client.get, {thisArg: client, multiArgs: true}); export default function* () { yield get('/some/path'); } And then in my test file I have something like this: import * as m from mymodule; it('should fail',

Firebase multiple async requests returned via Promises

早过忘川 提交于 2019-12-11 11:35:34
问题 I have a Firebase function that is attempting to take an array of UIDs and return an array of User Objects. I am trying to use Promise.all() to return all of the async results, but I am getting an empty array returned. I am, however, getting the logged out results after the fact. const fetchUserObjects = function(uids){ let promises = [] uids.forEach((uid) => { admin.database().ref(`/users/${uid}`).once("value") .then(function(dataSnapshot) { const userDataAll = dataSnapshot.val() const

Wait until nested promises resolve

杀马特。学长 韩版系。学妹 提交于 2019-12-11 10:44:31
问题 I am trying to make a bunch of nested calls to the database and want to do something once the results have all come back. I'm new to promises so forgive me if I'm totally doing this wrong (probably the case) My code currently is something like this: getVideos(user).then((videos) => { videos.forEach((video) => { getImage(video).then(() => { getMembers().then((members) => { getComments().then((comments) => { getKeywords().then((keywords) => { getTranscript().then((transcript) => { console.log

Make dynamically created promises execute in sequence

我怕爱的太早我们不能终老 提交于 2019-12-11 09:49:48
问题 How can I dynamically create a series of promises and have them execute in sequence? pseudocode for x=0 to maxValue promiseArray.push(createNewPromise(x)) executeAllPromisesSequentially(promiseArray) where executeAllPromisesSequentially is functionally equivalent to promise1() .then(promise2) .then(promise3) etc ... 回答1: There are some patterns displayed on my gist Promise Iteration with Reduce let tasks = [ /* ... */ ] let promise = tasks.reduce((prev, task) => { return prev.then(() => {

How to wait for function's promise in JavaScript

江枫思渺然 提交于 2019-12-11 09:46:46
问题 In my Node.js app I have a function that loops over an array of URLs and get their values, once loop is done it will return the final value as a Map. Code following. let getResults = function(urls){ let results = new Map(); let retrievePromises = []; urls.forEach(function (value, i) { retrievePromises.push( restAgent.get(value).then(function(data){ for (let item of data.items) { let name = item.filter(n => n.name === "somename"); //filter array let obj = {}; obj.name = item.name; obj.address

Is resolving a promise an asychronous process?

ⅰ亾dé卋堺 提交于 2019-12-11 09:45:55
问题 I am sure that all the codes below, except resolve(Promise.resolve(p2)) , are synchronous. So I expect the result is p2 first since the p2.then run first. However p1 comes out first in console. MDN has nothing related to the question. Does the spec has some details? Can someone make it clear step by step what happens in resolving a promise? Chrome v60.0.3112.113 My code is: var p1 = new Promise(function(resolve, reject){ resolve("p1"); }); var p2 = new Promise(function(resolve, reject){ /

Promisify a synchronous method

微笑、不失礼 提交于 2019-12-11 08:54:04
问题 Can I make a synchronous method into asynchronous by using promise? For example reading a file synchronously (yes there is fs.readFile which has callback): // Synchronous read var data = fs.readFileSync('input.txt'); Should I do this: function readFileAsync(){ return new Promise((resolve, reject) => { try { resolve(fs.readFileSync('input.txt')); } catch(err) { reject(err); } }) } or use async/await: function async readFileAsync(){ try { let result = await fs.readFileSync('input.txt'); return

Knex Seed: insert out of order from promise

泄露秘密 提交于 2019-12-11 07:29:35
问题 I'm trying to seed my knex data into my postgres database. However, I noticed that inserts from one Promise.all([...]) sometimes gets inserted into the previous or latter Promise.all([..]) table. exports.seed = function(knex, Promise) { return Promise.all([ knex('users').del(), knex('songs').del(), ]).then(() => { return Promise.all([ knex('users').insert({username: 'u1', password: '1', access: 'regular'}), knex('users').insert({username: 'u2', password: '1', access: 'regular'}), knex('users'

Generator not evaluating promises

╄→гoц情女王★ 提交于 2019-12-11 07:24:34
问题 Why doesn't the generator wait for the asynchronous prm promise to complete before moving on to the next yield? function *verify() { try { let prm = new Promise((resolve,reject) => { resolve("abc"); }) let k = yield prm console.log(k) yield 1; console.log("1") yield 2; console.log("2") yield 3; console.log("3") } catch (err) { console.log("error") } } var gen = verify() while (!gen.next().done) {} returns undefined 1 2 3 回答1: Because a generator - on its own - doesn't wait for anything. It

NodeJS - using Promises for API calls

早过忘川 提交于 2019-12-11 06:45:17
问题 I have a nodeJS library, where I'm coding a certain functionality, which will call a SOAP API to get information. I want that people can use the library easily. So that they can just call: library.requestThatService(parameters ...); And the library should handle all the dirty work behind scenes. What I want the library to do is to first possibly validate the parameters given. Then construct the message to be sent based on the parameters (serialization?), create signature etc... And finally,