sync-promise icon indicating copy to clipboard operation
sync-promise copied to clipboard

Implementing `Promise.resolve`/`Promise.reject` and `then(good, err)`

Open brettz9 opened this issue 10 years ago • 10 comments

Hi,

In your library, it is nice to see there is already a solution for the problem with promises for IndexedDB!

I would just suggest though that there are a few good reasons to support a couple of the features you rejected (no pun intended).

then(good, err) may be an anti-pattern, but I believe part of the appeal of your library is that it pretty much can work out of the box with existing promises however they are being used, and with a familiar syntax. The fewer the differences, the better, imo.

Implementing Promise.resolve and Promise.reject which simply adds a 0 second timeout (or animation frame perhaps) would seem to me to be in the spirit of these "immediately" returning. Besides allowing for existing Promises code to avoid the need for changes, it is a convenient practical wrapper of use in and of itself.

Thanks!

brettz9 avatar Apr 01 '16 00:04 brettz9

I haven't taken a close look yet, but wanted to mention immediate as an interesting sounding option for simulating a microtask...

brettz9 avatar Jun 13 '16 09:06 brettz9

Slightly shameful plug - I tried to use this library for a sync drop-in replacement of Promise and it failed. I created my own SyncPromise implementation, much less complete, intended to be a drop-in replacement - https://github.com/zbraniecki/syncpromise .

Hope that helps!

zbraniecki avatar Jul 14 '16 22:07 zbraniecki

@zbraniecki, what failed for you exactly?

brettz9 avatar Jul 14 '16 22:07 brettz9

@brettz9 - all Promise.resolve, and Promise.reject and the fact that this library throws on then when used on synchronously resolved promise, and that Promise.all requires there to be at least one promise in the list.

I needed something as close to "drop-in" replacement as possible and that's what I wrote. I guess my goals are just different :)

zbraniecki avatar Jul 15 '16 21:07 zbraniecki

Gotcha--I haven't looked at your code, but as an FYI, I also have worked around these issues within my fork at https://github.com/brettz9/sync-promise/tree/full-sync-missing-promise-features , but it also violates the principle of paldepind's library in sometimes working sync and sometimes not. However, I felt I had to do that because even with using immediate, the simulated microtask duration was not short enough for multiple promises to be chained within a transaction.

I wonder whether the transaction time is extended as long as a new operation takes place within a certain time window? And if so, I wonder whether there are any other options to simulating microtasks...

brettz9 avatar Jul 15 '16 21:07 brettz9

I needed a fully Sync Promise. Not a sync+async combo :)

zbraniecki avatar Jul 15 '16 21:07 zbraniecki

Actually, I think I misremembered--I believe mine is fully sync too :)

brettz9 avatar Jul 15 '16 21:07 brettz9

Cool! Did you also remove the limitation on not allowing to chain synchronous thens?

zbraniecki avatar Jul 15 '16 22:07 zbraniecki

Yes--including allowing catching synchronously thrown errors...

brettz9 avatar Jul 15 '16 22:07 brettz9

great! then you can remove the limitation from your README.md ;)

zbraniecki avatar Jul 15 '16 22:07 zbraniecki