This question already has answers here:
Wait for multiple promises to finish
(6 answers)
How do I access previous promise results in a .then() chain?
(17 answers)
How to return many Promises and wait for them all before doing other stuff
(6 answers)
Closed 2 years ago.
I have two Promises (let's say p and q) that will retrieve data when resolved, and I want to do something foo that needs the both data. It has a solution as coded below. But is there a more elegant one?
p.then(pData => {
q.then(qData => {
foo(pData,qData);
});
});
I was wondering something like follows (hypothetical function and):
p.and(q).then(dataArr => foo( ...dataArr ));
In the case there's no such way, is there anything in the promises concept that would disallow this construction?
Related
This question already has answers here:
How to wrap async function calls into a sync function in Node.js or Javascript?
(9 answers)
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
What is the JavaScript version of sleep()?
(91 answers)
Closed 1 year ago.
My function keeps executing indefinitely, although I would have expected it to resume after 10ms. Is there any way I can flush timeouts while I am waiting for the result? Or at least tell JS to execute / resolve other promises before I return my result?
The simple matter is, I NEED the response before I can continue execution. And I can not put my code in an async block whatsoever. I also can not use 'then' since I need to return the raw result. I thought this hack would work, but nope.
As far as my understanding of JS goes, this simply is not possible, but I would love to know.
let test = function () {
let wait = true;
setTimeout(() => {
wait = false
}, 10);
while (wait);
return wait;
}
you can use async/await , to block the flow of the function
example
const mine = async() => {
console.log("start")
await anotherAsyncFunction()
console.log("done")
}
This question already has answers here:
Return value from a Promise constructor
(2 answers)
Closed 2 years ago.
I am relatively new to javascript and want to find out how the things here work.
I don't understand why does it output
testing.js:2 Uncaught (in promise) Oops instead of catching this error and handling it.
Everything works fine if I just type reject(..)
Here is the code:
let alex = new Promise((resolve,reject)=>{
return Promise.reject('Oops');
})
.then((r)=>console.log(r))
.catch((err)=>console.log(err));
That's because you should use the two arguments received in the callback (resolve and reject). As you said, it works when you use reject('Oops') instead of return Promise.reject('Oops'). It should look like this:
let alex = new Promise((resolve,reject)=>{
reject('Oops');
})
.then((r)=>console.log(r))
.catch((err)=>console.log(err));
You can read more about promises here
This question already has answers here:
Wait until all promises complete even if some rejected
(20 answers)
Closed 2 years ago.
It seems Promise.all() resolves as soon as one of the promises fail, I would like to run all promises even if some reject. Is there a function for this?
You can use Promise.allSettled(), the new Promise API would resolve when all the promise objects in the supplied array are settled (i.e. either fulfilled or rejected).
The value in the then callback would have an array of the objects having two keys status and value describing the result of each individual promise in the given array:
Promise.allSettled([
Promise.resolve("Resolved Immediately"),
new Promise((res, rej) => {
setTimeout(res("Resolved after 3 secs"), 3000)
}),
Promise.reject(new Error("Rejected Immediately"))
]).then(arr => console.log(arr));
This question already has answers here:
How do I access previous promise results in a .then() chain?
(17 answers)
Closed 4 years ago.
Question related to EcmaScript:
const firstArg = 'myArg';
this._myFunction(firstArg)
.then((asyncResponse) => this._secondMethod(asyncResponse))
.then((asyncSecondResponse, asyncResponse) =>
this._thirdMethod(asyncSecondResponse, asyncResponse))
.then(() => this._lastMethod());
The problem is:
How to pass to the to the _thirdMethod 2 arguments (one from this._secondMethod(asyncResponse)) - that gives one argument and second is just previous asyncResponse) - I don’t want to define it inside _secondMethod(asyncResponse) to return these two arguments, just want to do it in this PIPE that I wrote above
Is there any syntax for it?
You can also nest the second "then" to keep the first asyncResponse in the scope
this._myFunction(firstArg)
.then((asyncResponse) => this._secondMethod(asyncResponse)
.then((asyncSecondResponse) => this._thirdMethod(asyncSecondResponse, asyncResponse)))
.then(() => this._lastMethod());
}
This question already has answers here:
How do I convert an existing callback API to promises?
(24 answers)
Closed 7 years ago.
I have a plugin I'm writing for a framework that uses Promises throughout.
The plugin is expected to return a Promise to process the event, and anything the plugin does during processing is chained before returning the next event.
I think...
Anyway - I am also using an older library that is completely callback based - and the callback doesn't follow the standard convention of having callback(err,resp) as the last parameter.
How do I turn this:
var call = libary.Api(_this.config.url, function(response){return new Promise() {Do my actions with the response..}}, _this.config.api_key);
return BbPromise.try(call).then(evt);
Into a Promise that depends on the callback being processed?
Try this:
return (new BbPromise(function(resolve) {
libary.Api(_this.config.url, function(response) {
resolve(response);
}, _this.config.api_key);
})).then(evt);