-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Promises #2556
Promises #2556
Conversation
@asturur @Kienz @inssein I'm wondering which other methods aside from |
i need time to understand this changes and undesired effects. |
@asturur I was confused about promises too, but they're actually very simple! It's a mechanism and API that allows us to transform callback-based methods to methods that can be chained instead. Here's an example: fabric.util.loadImage(url, function(img) { /* use img */ }); becomes: fabric.util.loadImage(url)
.then(function(img) { /* use img */ })
.catch(function() { /* something went wrong */ }) As you can see, Now, another great thing is that you can chain promises, so you can do: fabric.util.loadImage(url)
.then(function(img) { /* do something with an img*/ return img })
.then(function(img) { /* do more stuff */ })
.then(/* ... */) The advantages for us are:
|
This article explains it well — http://www.html5rocks.com/en/tutorials/es6/promises/ |
@kangax awesome! seems like you are already down the path of replacing almost all callbacks, which is what I would have suggested. I think it cleans up the code and allows better error handling. I will definitely be porting my application to use promises as well. Might be a little more painful for me as my extension of the image shape fires the image on load callback twice (once if a thumbnail is available and ready, and another one when the high res image is available). That said, I think using promises will clean up my implementation as well. |
Wait a second.... are we even using |
and _toDataURLWithMultiplier (but are last 2 used anywhere?)
@kangax any timeline on this? Would be a nice thing to get merged, along with the jsdom changes so we can be node > 4.2 compat. Going to wait until at least those two are merged before I stop using my own fork of fabric that uses the newer jsdom, but doesn't have all the recent changes. |
@inssein I just started a new job and have tons of stuff to do (again) so not sure when I'll have a chance to get to this! Sorry; will do my best but really no idea at the moment. |
I think that now that i got acquatained with promises ( finally.. ) i ll release 1.6.4 and start some beta branch with 2.0 Promises and some more changes. |
if (hasUrl) { | ||
svgCache.get(url, function (value) { | ||
var enlivedRecord = _enlivenCachedObject(value); | ||
resolve(enlivedRecord.objects, enlivedRecord.options); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not valid; a Promise can only ever be resolved with a single value. With the current approach, the second value would simply be lost. To resolve with multiple values, resolve with an object (with named keys) or an array.
Any update regards this PR?? i am struggling to get it clean when working with more than 20 canvas in a single react component. |
This PR should be rebased on latest version. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
We should add promises, this PR cannot be updated after so long, i ll close it. |
fromObject
to promises (no more different behavior for sync/async objects)fabric.util.loadImage
fabric.Image.fromURL
fabric.loadSVGFromURL
fabric.loadSVGFromString
clone
to make it consisten (similar tofromObject
)