es6-promise

TypeScript type definition for promise.reject

别说谁变了你拦得住时间么 提交于 2019-12-10 02:12:03
问题 The following code is correct in terms of the type that is returned, because then always return the promise array. Promise.resolve(['one', 'two']) .then( arr => { if( arr.indexOf('three') === -1 ) return Promise.reject( new Error('Where is three?') ); return Promise.resolve(arr); }) .catch( err => { console.log(err); // Error: where is three? }) TypeScript throw error: The type argument for type parameter 'TResult' cannot be inferred from the usage. Consider specifying the type arguments

How to convert method created to return a promise with $q library to use an ES6 Promise. Angularjs app to Angular4+

江枫思渺然 提交于 2019-12-09 23:48:08
问题 Since the ES6 Promise does not have a deferred object confused on how to go about converting this to work with ES6 Promises. One solution that I was looking at is to add a deffered object manually to the Promise Constructor. see the No deffered section for the example code Reason: I am converting the angularjs app to angular4 and using it to better understand how to work with ES6 Promises. I was going to add the deferred object, but thought that was too much of a workaround. function

Is it possible to wrap promise inside generator?

浪尽此生 提交于 2019-12-09 17:46:24
问题 I'm trying to create a promise-wrapper using generator so that I can do: var asyncResult = PromiseWrapper( $.ajax( ... ) ); So far, I've been trying with: function PromiseWrapper(promise){ return function *wrapper(promise){ promise.then(function(result){ yield result; }, function(err){ throw err; }); }(promise).next().value } but this fails because yielding inside a normal is not allowed. Is there any work-around for this? Thank you :D ps: I'm using babel to translate the code from es6 to es5

ES5 vs ES6 Promises

元气小坏坏 提交于 2019-12-09 14:17:48
问题 I wanna know whether JS promises were a part of es5? If so, why it doesn't work sometimes in older browsers and we have to add a polyfill for the same. Also, which polyfill should be added in that case, an es5 one or es6? I have a little confusion regarding the same. 回答1: ES5 did not have promises. Libraries like jQuery or Angular had their own custom and non-standard promise implementations. Popular Promise implementations for use with ES5 are Bluebird (which is compatible with the ES6

Get the data from fetch -> promise -> response

此生再无相见时 提交于 2019-12-09 10:38:13
问题 I am trying to post some data to the server but I don't know how to get back the response data. I have the following code: fetch(url, { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify({ email: login, password: password, }) }).then(function(a){ console.log(a); }) It prints a Response it contains data such as body (ReadableByteStream), bodyUsed (false), ok (true), status (200),... but I cannot find the data I get back, nowhere.

Ionic 2/Angular 2 promise returning observable

爱⌒轻易说出口 提交于 2019-12-09 10:36:47
问题 I have a situation where I need to fetch a piece of data from storage in an Ionic 2 application and then use that data to create an HTTP request. The problem that I am running into is that the SqlStorage methods return promises and the http meth returns an observables. I'm having to do something like this to get it to work: getToken() { return this.storage.get('token').then((token) => { this.token = token; return token; }); } loadStuff(){ return this.tokenService.getToken().then(token => {

How can I convert an onload promise into Async/Await

僤鯓⒐⒋嵵緔 提交于 2019-12-09 09:13:43
问题 I have the following Typescript that I would like to use async / await on. But I can't seem to sort it out in my head how to do this. private getWorkbookFromFile2(excelFile: File): Promise<xlsx.IWorkBook> { var loadedPromise = new Promise<xlsx.IWorkBook>((resolve, reject) => { var reader = new FileReader(); reader.onload = (event: any) => { var data = event.target.result; var workbook = xlsx.read(data, { type: 'binary' }); console.log(workbook.SheetNames); resolve(workbook); }; reader

Angular 2 do not refresh view after array push in ngOnInit promise

牧云@^-^@ 提交于 2019-12-09 03:31:03
问题 I created a NativeScript app with angular 2, i have an array of objects that i expect to see in the frontend of the application. the behaviour is that if i push an object into the array directly inside the ngOnInit() it works, but if i create a promise in the ngOnInit() it doesn't work. here is the code: export class DashboardComponent { stories: Story[] = []; pushArray() { let story:Story = new Story(1,1,"ASD", "pushed"); this.stories.push(story); } ngOnInit() { this.pushArray(); //this is

Create an array of fetch promises using a for loop with JavaScript/ES6 that can be read via Promise.all?

℡╲_俬逩灬. 提交于 2019-12-08 17:25:47
So, without boring anyone with the backstory, I need to access data from a number of APIs in order to run my script. The data needs to all be loaded before I execute the script, which I'm normally comfortable doing: I just declare some fetch requests, write a Promise.all, then continue on with the function. HOWEVER, I've hit something of a snafu with a certain API that limits the number of results I can pull from one request to 100 and I need to query all of the results. I didn't think this was a huge deal since I figured I can just make a couple extra requests by affixing "&page=X" to the end

What is a ZoneAwarePromise

天大地大妈咪最大 提交于 2019-12-08 17:03:49
问题 I am using angular 6. One of the http calls returns a ZoneAwarePromise when I try to convert the Observable into a Promise . Also the then block is not getting called. const login = this.authService.login(email, password).toPromise() login.then(() => {\* not getting called*\}) Can someone explain what is a ZoneAwarePromise ? 回答1: Angular relies heavily on zone.js to persist an execution context across asynchronous tasks. It's wrapped up in an injectable service called NgZone . These Zones