es6-promise

Creating a promise chain in a for loop

ε祈祈猫儿з 提交于 2019-11-28 19:24:32
I would expect the code below to print one number on the console, then wait a second and then print another number. Instead, it prints all 10 numbers immediately and then waits ten seconds. What is the correct way to create a promise chain that behaves as described? function getProm(v) { return new Promise(resolve => { console.log(v); resolve(); }) } function Wait() { return new Promise(r => setTimeout(r, 1000)) } function createChain() { let a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]; let chain = Promise.resolve(); for (let i of a) { chain.then(()=>getProm(i)) .then(Wait) } return chain; }

what happens if we dont resolve or reject the promise

爷,独闯天下 提交于 2019-11-28 19:05:29
I have a scenario where I am returning a promise. The promise basically give the response of a ajax request. On rejecting the promise it shows an error dialog that there is a server error. What I want to do is when the response code is 401, I neither want to resolve the promise nor reject it(cause it shows error dialog). I want to simply redirect to the login page. My code looks something like this function makeRequest(ur,params) { return new Promise(function(resolve,reject) { fetch(url,params) .then((response) => { let status = response.status; if (status >= 200 && status < 300) { response

Creating a (ES6) promise without starting to resolve it

人走茶凉 提交于 2019-11-28 16:57:54
Using ES6 promises, how do I create a promise without defining the logic for resolving it? Here's a basic example (some TypeScript): var promises = {}; function waitFor(key: string): Promise<any> { if (key in promises) { return promises[key]; } var promise = new Promise(resolve => { // But I don't want to try resolving anything here :( }); promises[key] = promise; return promise; } function resolveWith(key: string, value: any): void { promises[key].resolve(value); // Not valid :( } It's easily done with other promise libraries. JQuery's for example: var deferreds = {}; function waitFor(key:

Promise callbacks returning promises

為{幸葍}努か 提交于 2019-11-28 16:29:34
With regard to these great two sources: NZakas - Returning Promises in Promise Chains and MDN Promises , I would like to ask the following: Each time that we return a value from a promise fulfillment handler, how is that value passed to the new promise returned from that same handler? For instance, let p1 = new Promise(function(resolve, reject) { resolve(42); }); let p2 = new Promise(function(resolve, reject) { resolve(43); }); let p3 = p1.then(function(value) { // first fulfillment handler console.log(value); // 42 return p2; }); p3.then(function(value) { // second fulfillment handler console

Why does javascript ES6 Promises continue execution after a resolve?

◇◆丶佛笑我妖孽 提交于 2019-11-28 16:28:18
As I understand a promise is something that can resolve() or reject() but I was suprised to find out that code in the promise continues to execute after a resolve or reject is called. I considered resolve or reject being an async-friendly version of exit or return , that would halt all immediate function execution. Can someone explain the thought behind why the following example sometimes shows the console.log after a resolve call: var call = function() { return new Promise(function(resolve, reject) { resolve(); console.log("Doing more stuff, should not be visible after a resolve!"); }); };

What are the differences between observables and promises in JavaScript?

自古美人都是妖i 提交于 2019-11-28 16:27:07
问题 So i've read that observables are looking to overtake promises in terms of usage in some of upcoming JavaScript MVC's: Angular 2.0 Falcor used by Netflix What is the difference between observables and promises? Updated: Apologies! removed my falsy statement. 回答1: What is the difference between observables and promises? Simply put: A promise resolves to a single value asynchronously, an observable resolves to (or emits) multiple values asynchronously (over time). Concrete examples: Promise:

Why does the Promise constructor require a function that calls 'resolve' when complete, but 'then' does not - it returns a value instead?

…衆ロ難τιáo~ 提交于 2019-11-28 15:39:37
问题 As I plunge into studying Promise s, my understanding has halted on the following question that I do not find discussed (all I find are specific discussions of the Promise constructor, and the Promise ' then ' function - but not a discussion that compares their design patterns). 1. The Promise constructor From the MDN documentation, we have this use of the Promise constructor (with my comment added): new Promise(function(resolve, reject) { ... }); // <-- Call this Stage 1 Function object with

Resolve a promise once all internal concurrent promises have resolved or rejected

左心房为你撑大大i 提交于 2019-11-28 14:49:37
I am looking for something similar to Promise.all that will continue to resolve promises concurrently even in the event that one or more of the promises reject or throw an error. Each request does not rely on another request. Close to what I want - please see comments function fetchRequest (request) { return new Promise(function (resolve, reject) { fetch(request) .then(function(response) { return response.text(); }).then(function (responseXML) { //Do something here. Maybe add data to dom resolve(responseXML); }).catch(function (err) { reject(new Error(err)); } } function promiseRequests

Return fetch .json inside object.

本小妞迷上赌 提交于 2019-11-28 14:35:06
I have an API calling function that I would like to return the response.json() content as well as the response.status together in a single object. Like so: const getData = data => { return fetch('/api_endpoint',{ method: 'GET', headers: { 'Content-type': 'application/json' } }) .then(response => { return { body: response.json(), status: response.status } }) } The trouble is that response.json() is a promise, so I can't pull it's value until it's resolved. I can hack around it by doing this: const getData = data => { let statusRes = undefined; return fetch('/api_endpoint',{ method: 'GET',

How to create a function that returns an existing promise instead of new promise?

萝らか妹 提交于 2019-11-28 14:17:18
JavaScript/Promise experts, I hope you can help me, because I don't understand how I can create a function that returns an existing promise instead of a new promise. I have done a lot of research, but all examples return a new promise every time the function is called. Assume I have a function that takes some time to process and generates a different result every time it is being called. When I use the new promise method then I still need to wait to complete every time and I don't get the same value if I recall the function. I have included proof of concept I have created that works as desired