I have a nested promise. The promise resolves or rejects based on another promise resolving or rejecting with a setTimeout of 0 so as not to clog:
return new Promise((resolve, reject) => {
promiseInst
.then(value => {
executeSoon(() => {
dispatch({ ...action, status: "done", value: value });
resolve(value);
});
})
.catch(error => {
executeSoon(() => {
dispatch({
...action,
status: "error",
error: error.message || error
});
reject(error);
});
});
});
the executeSoon()
is executeSoon(fn) { setTimeout(fn, 0); }
How do I simplify this? Tried to get rid of the outer promise wrapper and returning the inner promise directly but got stuck with the resolve and reject not being defined. So I thought I'd just return a Promise.resolve(value)
from inside the .then and Promise.reject(error)
from the catch, but this does not work either. I feel it might be possible to simplify this, but can't quite get my head around it.
Remember that a
then
handler is always executed asychronously, there's no need forsetTimeout(..., 0)
in the normal case. So if thesetTimeout
doesn't have another purpose:If it does something important, I'd probably have a promise-enabled version, something along these lines:
Then see the lines with
***
below:Just use a promising timer:
Then its as simple as:
.... but actually you dont need the timer as promises are guaranteed to resolve one tick after
resolve()
was called.