es6-promise

React Native: synchronously run functions

北城以北 提交于 2019-12-23 16:22:46
问题 I'm new to OOP. My knowledge of promises/asynchronously/synchronously running functions is simply basic. I'd really appreciate your time and attention! This is the example from React-Native docs: async function getMoviesFromApi() { try { let response = await fetch('https://facebook.github.io/react-native/movies.json'); let responseJson = await response.json(); return responseJson.movies; } catch(error) { console.error(error); } } As i understand from code above, getMoviesFromApi is declared

Difference between supertest's expect and then?

筅森魡賤 提交于 2019-12-23 15:29:38
问题 When using supertest for testing async HTTP requests in JavaScript, what's the difference between these two snippets? Is one of them correct and the other one wrong? request('http://localhost:8080/').get('/api/people') .expect(res => res.body.should.have.length(5)) vs. request('http://localhost:8080/').get('/api/people') .then(res => res.body.should.have.length(5)) The only differences I could notice were: expect returns a Test object and, when test fails, prints a large stack trace then

Abort ecmascript7 async function

荒凉一梦 提交于 2019-12-23 12:34:05
问题 Is there a way to cancel a ES7 async function? In this example, on click, I want to abort async function call before calling new. async function draw(){ for(;;){ drawRandomRectOnCanvas(); await sleep(100); } } function sleep(t){ return new Promise(cb=>setTimeout(cb,t)); } let asyncCall; window.addEventListener('click', function(){ if(asyncCall) asyncCall.abort(); // this dont works clearCanvas(); asyncCall = draw(); }); 回答1: There's nothing built in to JavaScript yet, but you could easily

Synchronous or Sequential fetch in Service Worker [duplicate]

纵然是瞬间 提交于 2019-12-23 12:03:30
问题 This question already has answers here : Resolve promises one after another (i.e. in sequence)? (24 answers) Closed 4 years ago . I need to send a series of PUT & POST requests from a Service Worker. The order in which they're sent matters. Requirements: Given a request method, url, and JSON body, send the request If it succeeds ( response.status < 300 ): Pass body to a success function Call next request in queue If it fails: Pass responseText or err to error function Stop execution If I

ES6 promise execution order for returned values

走远了吗. 提交于 2019-12-23 07:38:14
问题 Trying to understand the order of execution of ES6 promises, I noticed the order of execution of chained handlers is affected by whether the previous handler returned a value or a promise. Example let a = Promise.resolve(); a.then(v => Promise.resolve("A")).then(v => console.log(v)); a.then(v => "B").then(v => console.log(v)); Output when run directly in Chrome (v 61) console: B A However, when clicking the Run code snippet button, I will get the order A B instead. Is the execution order

Unexpected end of JSON input on Promise resolve

被刻印的时光 ゝ 提交于 2019-12-23 05:16:43
问题 I am requesting weather data from openweather API and I get "error SyntaxError: Unexpected end of JSON input at Object.parse ()" every time my promise is resolved and I do JSON.pase(daat) The JSON I get looks like this: { "coord":{"lon":24.94,"lat":60.17}, "weather":[{"id":741,"main":"Fog","description":"fog","icon":"50n"}], "base":"stations", "main":{"temp":273.15,"pressure":994,"humidity":94,"temp_min":273.15,"temp_max":273.15}, "visibility":500, "wind":{"speed":1.5}, "clouds":{"all":90},

Angular 4 chaining http get request(Promises)

心不动则不痛 提交于 2019-12-23 04:24:09
问题 I would like to receive some help on the following code: fillDataAssociationsArray(DataAssociationIDS: string[]) { const promise = new Promise((resolve, reject) => { for (const id of DataAssociationIDS) { this.api.getDataAssociationByID(id).toPromise().then( data => { this.DataAssociations.push(data.body.entities.DATAASSOCIATIONS[0]); }); } resolve(); }); return promise;} In the code above I am trying to convert an array of DataAssociationIDS to DataAssociation objects, and than pushing them

How do I use Promise.all() with chrome.storage()?

走远了吗. 提交于 2019-12-23 02:38:37
问题 I have several async functions running. I want to wait for them all to finish before taking the next steps. Here's my code that I'm using to get all of the key/values from chrome.storage and the Promise.all() implementation. var promise1 = Promise.resolve(3); var promise2 = 42; var promise3 = new Promise(function(resolve, reject) { setTimeout(resolve, 100, 'foo'); }); var getAll = chrome.storage.sync.get(function(result) { console.log(result) }); Promise.all([promise1, promise2, promise3,

Unit testing logic inside promise callback

Deadly 提交于 2019-12-22 12:23:24
问题 I have an ES6 / Aurelia app that I am using jasmine to test. The method I am trying to test looks something like this: update() { let vm = this; vm.getData() .then((response) => { vm.processData(response); }); } Where this.getData is a function that returns a promise. My spec file looks something like this: describe('my service update function', () => { it('it will call the other functions', () => { myService = new MyService(); spyOn(myService, 'getData').and.callFake(function() { return new

Break a Promise chain inside for-loop

旧时模样 提交于 2019-12-22 11:16:22
问题 I am working on a promise chain that was inspired by this answer: https://stackoverflow.com/a/44955506/7485805 I want to break this for loop in order to properly handle the rejection of the chain. I just figured that I can't use break inside the .catch method of the chain. Here's my code if it helps: function pro (arr) { let chain = Promise.resolve(); const self = {req: {}, res: {}}; const length = arr.length; return new Promise((resolve, reject) => { for(let i=0; i<length; i++){ chain =