promise

How does Promise in javascript work under the hood? My Promise implementation doesn't work the same

假装没事ソ 提交于 2021-02-07 09:31:05
问题 I am a newbie to JS and I am trying to understand how Promise should work under the hood. Here is a custom implementation that looks reasonably good to me: class MyPromise { constructor(executor) { this._resolutionQueue = []; this._rejectionQueue = []; this._state = 'pending'; this._value; this._rejectionReason; try { executor(this._resolve.bind(this), this._reject.bind(this)); } catch (e) { this._reject(e); } } _runRejectionHandlers() { while(this._rejectionQueue.length > 0) { var rejection

Recursively fetching data with axios and chaining the result

元气小坏坏 提交于 2021-02-07 09:24:54
问题 I have an url of the pattern http://www.data.com/1 , where the 1 at the end can run up until a not predefined number. It returns an array. I need to concatenate all the arrays I get into one. My strategy is to recursively perform a get-request, until I get a 404 error, upon which I return the result. var returnArray = []; function getData(count){ let p = axios.get(`http://www.data.com/${count}`).then(function(response){ returnArray = returnArray.concat(response.data); return getData(count +1)

Why does this async function execute before the equivalent Promise.then chain defined before it?

人走茶凉 提交于 2021-02-07 04:56:43
问题 I have the following code: var incr = num => new Promise(resolve => { resolve(num + 1); }); var x = incr(3) .then(resp => incr(resp)) .then(resp => console.log(resp)); async function incrTwice(num) { const first = await incr(num); const twice = await incr(first); console.log(twice); } incrTwice(6); Which I believe (perhaps mistakenly) shows two equivalent ways to achieve the same functionality: first by chaining promises and second with the syntactic sugar of async/await. I would expect the

tracking request flow by ID in node.js

心不动则不痛 提交于 2021-02-07 03:55:18
问题 In my node.js application whenever I get request I'd like to "stamp" it with some unique ID and be able to track all the activities (log statements) related to this request by this ID. So when request comes in and I pass its handing to lower application layers (services, database calls etc) I want to be able to collect all logs matching given request ID to rebuild its journey through the app. So having this picture, I use request-local module now, but it does some heavy magic that has just

tracking request flow by ID in node.js

六月ゝ 毕业季﹏ 提交于 2021-02-07 03:54:41
问题 In my node.js application whenever I get request I'd like to "stamp" it with some unique ID and be able to track all the activities (log statements) related to this request by this ID. So when request comes in and I pass its handing to lower application layers (services, database calls etc) I want to be able to collect all logs matching given request ID to rebuild its journey through the app. So having this picture, I use request-local module now, but it does some heavy magic that has just

How to make promise.all wait for nested promise.all?

不打扰是莪最后的温柔 提交于 2021-02-07 03:41:11
问题 So I have a problem with JavaScript Promises. I'm using native implementation for the sake of reducing dependencies. An illustrative example of what I need. I need to retrieve lists of books, book authors and purchases. I also need an author profile for each of the authors. After I got all of that, I need to create a nice set of Authors with their books and purchase list for each of the books. Lists and profiles are separate API JSON calls. The only dependency is that I need a list of authors

How to make promise.all wait for nested promise.all?

痞子三分冷 提交于 2021-02-07 03:40:13
问题 So I have a problem with JavaScript Promises. I'm using native implementation for the sake of reducing dependencies. An illustrative example of what I need. I need to retrieve lists of books, book authors and purchases. I also need an author profile for each of the authors. After I got all of that, I need to create a nice set of Authors with their books and purchase list for each of the books. Lists and profiles are separate API JSON calls. The only dependency is that I need a list of authors

How do you know when an indefinitely long promise chain has completely finished?

╄→尐↘猪︶ㄣ 提交于 2021-02-07 03:28:43
问题 I was trying to use promises to force serialization of a series of Ajax calls. These Ajax calls are made one for each time a user presses a button. I can successfully serialize the operations like this: // sample async function // real-world this is an Ajax call function delay(val) { log("start: ", val); return new Promise(function(resolve) { setTimeout(function() { log("end: ", val); resolve(); }, 500); }); } // initialize p to a resolved promise var p = Promise.resolve(); var v = 1; // each

Wrapping promise in async/await

家住魔仙堡 提交于 2021-02-07 03:22:22
问题 I'm struggling a bit with async/await and returning a value from a Promise. function test () { return new Promise((resolve, reject) => { resolve('Hello') }) } async function c() { await test() } As I understood things I should be able to get a value by doing: console.log(c()) But clearly I am missing a point here as this returns a promise. Shouldn't it print "hello"? On a similar note I am unclear as to whether a callback needs to be converted to a promise before wrapping it in async/await?

Wrapping promise in async/await

ぐ巨炮叔叔 提交于 2021-02-07 03:22:02
问题 I'm struggling a bit with async/await and returning a value from a Promise. function test () { return new Promise((resolve, reject) => { resolve('Hello') }) } async function c() { await test() } As I understood things I should be able to get a value by doing: console.log(c()) But clearly I am missing a point here as this returns a promise. Shouldn't it print "hello"? On a similar note I am unclear as to whether a callback needs to be converted to a promise before wrapping it in async/await?