es6-promise

When is the body of a Promise executed?

ⅰ亾dé卋堺 提交于 2019-11-28 08:53:22
Suppose I have the following Promise : function doSomethingAsynchronous() { return new Promise((resolve) => { const result = doSomeWork(); setTimeout(() => { resolve(result); }), 100); }); } At which point in time is doSomeWork() called? Is it immediately after or as the Promise is constructed? If not, is there something additional I need to do explicitly to make sure the body of the Promise is run? Immediately, yes, by specification. From the MDN : The executor function is executed immediately by the Promise implementation, passing resolve and reject functions (the executor is called before

ES6 Promises - Calling synchronous functions within promise chain

柔情痞子 提交于 2019-11-28 07:35:34
I'm currently experimenting with promises and have a really basic question! Within a promise chain, would it be bad practice to call a synchronous function? For example: .then(function(results) { if(checkIfResultInMemory(results) === true){ return getTotalFromMemory() } return results; }) Or should my sync functions be refactored to return promises also? Within a promise chain, would it be bad practice to call a synchronous function? No, it is not a bad practice at all. It is one of many expected and useful practices. You are perfectly free to call either synchronous functions within the

How do I handle multiple browser scripts making the same calls to the back-end service

╄→尐↘猪︶ㄣ 提交于 2019-11-28 05:59:32
问题 I have a web page where different parts of it all need the same back-end data. Each is isolated, so they each end up eventually making the same calls to the back-end. What is the best way to avoid making a call to the web server when one is already in progress and initiated by a different piece of code on the same web page? Here's an example. I'll use setTimeout to simulate an asynchronous call. Let's assume there's an async function that returns the list of contacts, which is basically a

How to use promise in forEach loop of array to populate an object

不问归期 提交于 2019-11-28 05:55:56
I am running a forEach loop on an array and making two calls which return promises, and I want to populate an object say this.options , and then do other stuff with it. Right now I am running into the async issue if i use the following code sample and i get into the then function first. $.when.apply($, someArray.map(function(item) { return $.ajax({...}).then(function(data){...}); })).then(function() { // all ajax calls done now }); This is working code below, but it only works for the first element in the array, because I call the resulting function in the .then of the response. I want to do

Does every ECMAScript runtime with await await any Thenable? [duplicate]

爷,独闯天下 提交于 2019-11-28 04:47:21
问题 This question already has answers here : Does await await promise-like objects? [duplicate] (2 answers) Custom thenables: Can I create my own objects with a “then” method? (1 answer) Closed last year . The following currently logs Yahtzee in node, chrome, firefox. As you can see, not even prototype of Promise was set. const fake = new Number(1) fake.then = fn => setTimeout(fn, 0, 'Yahtzee') const main = async () => { console.log(await fake) } main() Does this works universally? And more

Are JavaScript ES6 Classes of any use with asynchronous code bases?

旧巷老猫 提交于 2019-11-28 04:37:55
What can ES6 Classes provide, as a pattern of organization, to asynchronous code. Below is an example with ES7 async/await, can an ES6-class have an asynchronous method, or constructor in ES7? Can I do: class Foo { async constructor() { let res = await getHTML(); this.res = res } } And, if not how should a constructor work that does this? class Foo { constructor() { getHTML().then( function (res) { this.res = res } } } If neither of these patterns work, can a constructor (and moreover classes) in an ES6 class support any form of asynchronicity that operates on the object's state? Or, are they

NodeJS, Promises and performance

最后都变了- 提交于 2019-11-28 01:40:22
My question is about performance in my NodeJS app... If my program run 12 iteration of 1.250.000 each = 15.000.000 iterations all together - it takes dedicated servers at Amazon the following time to process: r3.large: 2 vCPU, 6.5 ECU, 15 GB memory --> 123 minutes 4.8xlarge: 36 vCPU, 132 ECU, 60 GB memory --> 102 minutes I have some code similair to the code below... start(); start(){ for(var i=0; i<12; i++){ function2(); // Iterates over a collection - which contains data split up in intervals - by date intervals. This function is actually also recursive - due to the fact - that is run

JS ES6 Promise Chaining

蓝咒 提交于 2019-11-28 00:14:38
问题 I'm trying to learn how to use promises, but am having trouble comprehending the chaining. I assume that with this code, both promises will run. Then when I call test.then() it should know that test has resolved and pass the resolve data to then(). Once that function finishes, it goes onto the next then(), repeating the same process with the test2 promise. However, I can only get it to print out the first promise results, not the second. Any ideas what is missing here? var test = new Promise

How to measure the execution time of a promise?

半腔热情 提交于 2019-11-28 00:10:04
问题 I'm trying to write a function that measures the execution time of another function: export class Profiler { public measureSyncFunc(fn: () => any): Promise<number> { return new Promise<number>((resolve, reject) => { let elapsed = 0; let intervalId = window.setInterval(() => { elapsed += 1; // this is never called }, 1); this.execFunc(fn) .then((result: any) => { window.clearInterval(intervalId); resolve(elapsed); }); }); } private execFunc(fn: () => any): Promise<any> { return new Promise<any

Are JavaScript forever-pending promises bad?

若如初见. 提交于 2019-11-27 23:55:57
Say I have a promise called myProm , and say I have success and error handlers called onSuccess and onError . Whenever my promise takes longer than 10 seconds to complete, I want a function called timeoutHandler to be executed, but if that happens, neither onSuccess nor onError should be executed. (Similarly, if either onSuccess or onError runs, I don't want my timeoutHandler to be executed.) I've come up with the following snippet for this. new Promise((suc, err) => { let outOfTime = false; const timeoutId = window.setTimeout(() => { outOfTime = true; timeoutHandler(); }, 10000); myProm.then(