es6-promise

What will happen when return new Promise((resolve, reject) => {}) forgot to call either resolve or reject? [duplicate]

强颜欢笑 提交于 2019-12-04 20:25:44
This question already has an answer here: Are JavaScript forever-pending promises bad? 2 answers The problem is like this function demo() { return new Promise((resolve, reject) => { ... // The problem here!! //I just found in some rare case we failed to call resolve or reject }) } demo() .then(res => { console.log('resolve') console.log(res) }) .catch(rej => { console.log('reject') console.log(rej) }) .finally(() => { console.log('why') }) When I failed to call resolve or reject, even the finally block is not called! Why ? I had thought it was a bug then I found the original author seemed to

Promises es6 and superagent

☆樱花仙子☆ 提交于 2019-12-04 18:30:27
问题 I'm attempting to use es6 promises with superagent. I'm attempting to call a function that has a superagent request wrapped inside. Request.post(buildReq).then(res => { if (res.ok) {//process res} }); Here is the function wrapping superagent static post(params) { superagent .post(params.url) .send(params.payload) .set('Accept', 'application/json') .end((error, res) => { return this.Promise.resolve(res); }) .bind(this); } I'm getting an error enter code here Uncaught TypeError: Cannot read

Javascript Promise push value into array (only from function or outside?)

回眸只為那壹抹淺笑 提交于 2019-12-04 17:02:43
I have some promises and a Promise.all: array = []; var one = new Promise(function(resolve, reject) { // Do stuff setTimeout(function() { resolve('One Done'); array.push('one'); }, 5000); }); var two = new Promise(function(resolve, reject) { // Do Stuff resolve('Two Done'); array.push('two'); }); Promise.all(array).then(values => { console.log(values); }); We know this doesn't work because array.push needs to be outside. I currently have a few functions which I need to have called by promises so that finally I can have it in Promise.all. Would it be advisable to call the function from inside

Using Fetch API with Promise.all

可紊 提交于 2019-12-04 16:53:20
my aim is to fetch data from two URLs and perform an action only when both have come back successfully. On the other hand i want to return an error if either of them fail. I have played around with my code and managed to get the desired effect. My question is, is there a more efficient, succinct way of achieving the same functionality? Helper functions let status = (r) => { if (r.ok) { return Promise.resolve(r) } else { return Promise.reject(new Error(r.statusText)) } } let json = (r) => r.json(); Requests let urls = [ 'http://localhost:3000/incomplete', 'http://localhost:3000/complete' ] let

JavaScript Promises and setTimeout

孤街浪徒 提交于 2019-12-04 15:45:59
问题 I have been playing with Promises, but I am having trouble understanding what is happening with the following code: function a() { return new Promise(function (resolve, reject) { resolve("hi from a!"); }); } function b() { return new Promise(function (resolve, reject) { setTimeout(function () { resolve("hi from b!"); }, 5000); }); } function c() { return new Promise(function (resolve, reject) { setTimeout(function () { resolve("hi from c!"); }, 1000); }); } a().then(function (resultFromA) {

JavaScript async/await for Promises inside Array.map() [duplicate]

依然范特西╮ 提交于 2019-12-04 15:05:41
This question already has an answer here: Using async/await with a forEach loop 15 answers Given the following code class SomeClass { async someFunc() { const urlParameters = [0, 1, 2]; const batchAjaxResult = await urlParameters.map((parameter) => { const result = await $.get(`someUrl/${parameter}`); return { parameter, result } }); console.log(batchAjaxResult); } } JavaScript will return an Array of resolved Promises instead of the actual Promises result. This is probably due to Array.map() not being implemented as a Promise. Is there a Promise-based version of Array.map ? This is question

Uncaught (in promise) TypeError: Cannot read property 'setState' of undefined [duplicate]

做~自己de王妃 提交于 2019-12-04 11:50:35
问题 This question already has answers here : How to access the correct `this` inside a callback? (10 answers) Closed 2 years ago . For me this error is quite often when using axios. I can't setstate with undefined property. Eventhough i am getting actual response. I am pretty confused. Any solution would be appreciated. json reply by axios reply [ { main: 1, left: 0, right: 0, top: 0, bottom: 0, cid: 6, '$created': '2016-10-21T11:08:08.853Z', '$updated': '2016-10-22T07:02:46.662Z', stop: 0 } ]

Retry on Javascript.Promise.reject a limited number of times or until success

匆匆过客 提交于 2019-12-04 10:53:09
I have a function say myMainFunction that is called from a client, that in turn calls mypromisified function. Scenario: mypromisified function can fail intermittently and I need to call this function with a delay (at an exponential increase) until success or until max no of tries reached. What I have so far The following code illustrates my scenario and repeats itself until success, but it tries indefinitely and not until certain count is reached // called once from the client myMainFuntion(); function rejectDelay(delay, reason) { // call main function at a delayed interval until success //

Promise.allSettled in babel ES6 implementation

旧街凉风 提交于 2019-12-04 10:26:33
问题 I'm using babel to transpile my node.js@0.10.x code and I'm stuck with promises. I need allSettled -type functionality that I could use in q and bluebird or angular.$q for example. On babel's core-js Promise , there is no allSettled method. Currently I'm using q.allSettled as a workaround: import { allSettled } from 'q'; Is there something like that in babel polyfill? Alternatively, which is a good algorithm for me to try to implement? 回答1: Alternatively, which is a good algorithm for me to

Javascript: Run async task in series(or sequence) without libraries

雨燕双飞 提交于 2019-12-04 10:24:14
I want to run some asynchronous task in a loop, but it should execute in sequence order(one after another). It should be vanilla JS, not with any libraries. var doSome = function(i) { return Promise.resolve(setTimeout(() => { console.log('done... ' + i) }, 1000 * (i%3))); } var looper = function() { var p = Promise.resolve(); [1,2,3].forEach((n) => { p = p.then(() => doSome(n)) }) return p; } looper(); Current output: calling for ...1 calling for ...2 calling for ...3 Promise {<resolved>: 8260} done... 3 done... 1 done... 2 Expected output: calling for ...1 calling for ...2 calling for ...3