es6-promise

Typescript cannot find name 'Promise' despite using ECMAScript 6

本秂侑毒 提交于 2019-12-14 01:18:30
问题 I am currently trying to develop a node.js application in Visual Studio 2015 and it keeps telling me TS2304 Cannot find the name 'Promise' In the project settings I have ECMAScript 6 configured as build system and ES 2015 as module system. I already tried how to use es6-promises with typescript? How to use Typescript with native ES6 Promises without success, but in the second link they say that it should work with ECMAScript version set to 6, but it changes nothing for me. EDIT : I now did

.then Executes on 'pending' Rather than Resolved Promise

情到浓时终转凉″ 提交于 2019-12-13 17:16:00
问题 Why does the following code log an array of pending promises? I expect all promises in the array to be resolved and then execute the functions inside the .then methods. for (let i = 0; i < limit; i++) { promiseArray.push(fetch(someUrls[i])) } Promise.all(promiseArray) .then(responses => responses.map(response => response.text())) .then(result => console.log(result)) Thanks in advance 回答1: That's because response.text() method returns a promise. fetch is a 2-promise thing. One is for the

Best way to wait for 3rd-party JS library to finish initializing within Angular 2 service?

时间秒杀一切 提交于 2019-12-13 15:26:40
问题 I'm wrapping a 3rd-party JS library in an Angular service. How can I guarantee that the 3rd-party JS library is loaded/initialized/etc. before using my service? Can you take a look at the following code and tell me if this is a good coding practice ? I'm setting up a promise in the service's constructor (promise that will eventually be fulfilled) and I have the service methods depend on the success of that promise (see getMessages ). @Injectable() export class GmailService { private

How to get response data outside promise chain of Fetch? [duplicate]

十年热恋 提交于 2019-12-13 12:48:08
问题 This question already has answers here : How do I return the response from an asynchronous call? (36 answers) Closed 2 years ago . I'm using fetchApi to get data from my API. In my RequestHelpers.js, I do this code module.exports = { fetchQuizCollection(){ return fetch(HOST+API_KEY) .then((response) => response.json()) .then((responseData) => { let gameData = responseData console.log(responseData) //It work return responseData }) .done(); } } And I call that function in another file, I couldn

value from promise is not being exported to another module

北城以北 提交于 2019-12-13 10:30:38
问题 https://jsfiddle.net/oc5v4bs5/ <==link to the code when exporting accToken variable, it is showing undefined value. why is this showing? //core modules const OAuth2 = require('oauth').OAuth2; //vars const clientId = '<myClientId>'; const clientSecret = '<myClientSecret>'; let accToken; const oauth2 = new OAuth2( clientId, clientSecret, 'https://accounts.spotify.com/', null, 'api/token', null); //make gotAuth promise const gotAuth = new Promise((resolve,reject)=>{ oauth2.getOAuthAccessToken(''

How to launch a class function on promise in Javascript?

我怕爱的太早我们不能终老 提交于 2019-12-13 10:15:53
问题 I have the following class: let graphFactory = new GraphFactory(); function GraphFactory(){ let value = 'something'; this.release = function() { return value; } } Now, when I try to call this function from another place in my program in this way: let newvalue = graphFactory.release(); It does not recognize graphFactory because it takes some time to load this function. I would like to resolve this by issuing a promise when graphFactory is fully loaded and activated, but when I tried to add a

Promise then and catch clauses not working

你离开我真会死。 提交于 2019-12-13 08:10:55
问题 Background I have a multitude of functions that may or may not return a Promise and that may or may not fail, like in the examples below: let goodFun = (fruit) => `I like ${fruit}`; let badFun = (fruit) => { throw `${fruit} is spoiled!`; }; let badPromise = (fruit) => new Promise( (fulfil, reject) => { reject(`Promise failed with ${fruit}`); }); let goodPromise = (fruit) => new Promise((fulfil, reject) => { fulfil(`Promise succeeded with ${fruit}`); }); Because each function may or may not

Promise catch behavior

梦想与她 提交于 2019-12-13 07:53:29
问题 it seems the following works without throwing an error: var p = new Promise (function (resolve, reject) { window.setTimeout(function() { reject('ko'); }, 1000); }); p.then(function (value) { console.log(value); }) .catch(function () { console.log('catched'); }); // → 'catched' But this throws an error: var p = new Promise (function (resolve, reject) { window.setTimeout(function() { p.catch(function () { console.log('catched'); }); reject('ko'); }, 1000); }); p.then(function (value) { console

Use promises instead of callbacks

空扰寡人 提交于 2019-12-13 06:49:40
问题 I've completed this exercise from learnyounode and I'm trying to refactor it using ES2015's promises (or with other libraries if it's easier). I've read about promises and I think I understand how they work, but I'd like to know if it's possible to use them in the following code and how to do it. My goal is to make the code easier to read and understand, and also better understand promises in the process. let http = require("http"); if (process.argv.length != 5) { throw new Error("Please

How can a map an asynchronous function over a list?

会有一股神秘感。 提交于 2019-12-13 06:39:06
问题 Obviously, given a list l and an function f that returns a promise, I could do this: Promise.all(l.map(f)); The hard part is, I need to map each element, in order . That is, the mapping of the first element must be resolved before the the next one is even started. I want to prevent any parallelism. I have an idea how to do this, which I will give as an answer, but I am not sure it's a good answer. Edit: some people are under the impression that since Javascript is itself single-threaded,