es6-promise

How to retrieve all posts of a user via Facebook Graph API using promises and recursion?

时光总嘲笑我的痴心妄想 提交于 2019-12-08 02:56:32
问题 I am currently developing a web app which uses the Facebook Graph API. What I would like to achieve is to get all posts of a user. However, this is not that easy since I have to paginate the results. At the moment I am struggeling with promises. What I try to achieve is to fill an array with the post objects. Therefore I use promises and recursion which does not work as expected. My code currently looks as follows: // Here I retrieve the user with his or her posts, // just the first 25 due to

How do I make ES6 generators wait for promises, like in redux-saga?

微笑、不失礼 提交于 2019-12-08 02:14:33
问题 I've read that generators don't wait for promises. How come this is not the case with generators in redux-saga , and how do I make my own generators wait? For example, this saga: takeLatest('FETCH_USER_REQUESTED', function*() { const fetchPromise = yield put(fetchUser()); const user = yield fetchPromise; console.log(user) yield 1 console.log(1) }) will output: Promise Object // <= user data fetched asynchronously 1 instead of: Promise undefined 1 回答1: How come this is not the case with

I can't get the value of “result” in Node.js

ⅰ亾dé卋堺 提交于 2019-12-08 01:45:14
问题 In my console.log(info) , I want to get the value of "result". But when I use console.log(info) , I get Promise { <pending> } : var info=new Promise((resolve, reject) => { request(options, (err, res, body) => { if (body.match('success') || body.match('code="25"')) { resolve("success"); } else { reject(body); } }); }).then(result => { return result; }).catch(err => { console.log("error: " + err) }); console.log(info); I would like to get info == result . How can I do it? Thanks 回答1: What

Angular 2 chained Promise and passing on reject

社会主义新天地 提交于 2019-12-08 00:38:59
问题 Should be a simple question, however I can find no documentation on how to do it. Chaining a promise like this: // Making a promise, no problem let promise = new Promise((resolve, reject) => { let data = {}; // do something reject(data); }); // First chain, no problem promise.then(data => { console.log('Success callback', data); // This will not be called }).catch(err => { console.log('Error callback', err); // This WILL be called }) // Second chain, acts as resolved .then(data => { console

how do I perform a large batch of promises? [duplicate]

北城以北 提交于 2019-12-07 23:44:41
问题 This question already has answers here : ES6 Promise replacement of async.eachLimit / async.mapLimit (3 answers) Closed 11 months ago . I am planning on running a large number of queries on firebase which could grow to the order of a few hundred thousand to even millions. I've been using Promise.all() to resolve most of my queries but as the requests grow Promise.all() seems to just stop running at a random number.Ive looked into using Promise.map() but Im not sure if the concurrency will

Resolving a Promise without calling the 'then'

吃可爱长大的小学妹 提交于 2019-12-07 17:49:16
问题 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

Await for asynchronous function results in undefined

主宰稳场 提交于 2019-12-07 13:08:51
问题 I'm having trouble with async/await with Node. When I try this: function Read_Json_File() { fs.readFile('import.json','utf-8',(err, data) => { if (err) throw err; json_data = JSON.parse(data); return json_data; }); } async function run_demo() { let get_json_file = await Read_Json_File(); console.log(get_json_file); } run_demo(); It returns undefined instead of the JSON from the file. Why doesn't it wait for the file reading to finish? 回答1: You're not returning anything from Read_Json_File ,

Updating DOM before blocking code by setTimeout or promise

亡梦爱人 提交于 2019-12-07 11:41:35
问题 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){

Make an Api call with javascript promises in recursion

我们两清 提交于 2019-12-07 10:28:16
问题 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

using await on global scope without async keyword

℡╲_俬逩灬. 提交于 2019-12-07 04:20:33
问题 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 回答1: 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() {