sync-resolve
v0.0.1
Published
Synchronously resolve promises
Downloads
4
Maintainers
Readme
sync-resolve
Sometimes, you know that a promise can be resolved synchronously, and you have a genuine need to resolve it synchronously, but you can't. Promises/A+ purposely don't allow synchronous resolution of then
invocations because call-back APIs that may or may not resolve asynchronously lead to bugs for users of such APIs. However, if a piece of code can always be resolved synchronously, and we wrap a synchronous API around it, then we've got an API that's always synchronous, and so there are no problems.
Here's a concrete example from SystemJS, where all libraries had been pre-bundled, and so the author knew that it was now safe for any System.import()
invocations to be resolved synchronously. With sync-resolve we can write synchronous code like this:
const React = syncResolve(() => System.import('react')).default;
instead of having to write asynchronous code like this:
System.import('react').then((m) => {
const React = m.default;
});
We achieve this by using the synchronous-inspection feature of Bluebird together with a forced synchronous-resolution mechanism that we add ourselves.