JavaScript Is there any way to resolve a promise synchronously? (or an alternative library that can)

You mention two different things:

  1. I want the interface to remain consistent

  2. [I want to] always return a Promise

If you want to avoid the asynchronous behaviour if it is not needed, you can do that and keep the API consistent. But what you cannot do is to "always return a Promise" as it is not possible to "resolve a promise synchronously".

Your code currently returns a Promise that is resolved when there is no need for an async validation:

// Otherwise return a resolved promise (to provide a consistent interface)
return Promise.resolve(); // Synchronous validation passed

You can replace that code with the following:

return {then: cb => cb()};

Note that this just returns an object literal that is "thenable" (i.e. it has a then method) and will synchronously execute whatever callback you pass it to. However, it does not return a promise.

You could also extend this approach by implementing the optional onRejected parameter of the then method and/or the the catch method.

Answer:1

The reason why promises resolve asynchronously is so that they don't blow up the stack. Consider the following stack safe code which uses promises.

console.time("promises");

let promise = Promise.resolve(0);

for (let i = 0; i < 1e7; i++) promise = promise.then(x => x + 1);

promise.then(x => {
    console.log(x);
    console.timeEnd("promises");
});
Answer:2

I have an array of data objects about people. Each person object includes 0-n URLs for additional info (guests of the person). I want to process this list, calling each of the 'guest' URLs and ...

I have an array of data objects about people. Each person object includes 0-n URLs for additional info (guests of the person). I want to process this list, calling each of the 'guest' URLs and ...

Performing For..Loops or While..Loops does not update the $scope variable. I'm trying to show the progress as loops are incremented. I have read (http://jimhoskins.com/2012/12/17/angularjs-and-apply....

Performing For..Loops or While..Loops does not update the $scope variable. I'm trying to show the progress as loops are incremented. I have read (http://jimhoskins.com/2012/12/17/angularjs-and-apply....

  1. angularjs loop not updating

I'm studying rxjs Observables and I couldn't understand why the setInterval inside Observable.create is still running even I have unsubscribe to that observable object. Observable is async right? When ...

I'm studying rxjs Observables and I couldn't understand why the setInterval inside Observable.create is still running even I have unsubscribe to that observable object. Observable is async right? When ...

I have a simple set up to find top scores. The goal is to order who received the highest scores and then give the top 3 bonus points. Question: In part two below I am looping through the people and ...

I have a simple set up to find top scores. The goal is to order who received the highest scores and then give the top 3 bonus points. Question: In part two below I am looping through the people and ...