jariz / vibrant.js

Extract prominent colors from an image. JS port of Android's Palette.
http://jariz.github.io/vibrant.js/
MIT License
4.61k stars 237 forks source link

Async? #13

Closed rayshan closed 9 years ago

rayshan commented 9 years ago

Hello, is there a way to do the processing asynchronously?

watdafox commented 9 years ago

we use Q for that

jariz commented 9 years ago

@watdafox Elaborate?

watdafox commented 9 years ago

Well, on our project, we use Q to make the entire process a promise.

1) Call a custom function where vibrantjs does its magic (behind a Q.defer). 2) Wait for the promise to be completed before continuing with vibrantjs data.


In application: https://git.popcorntime.io/popcorntime/desktop/blob/feature/materialDesign/src/app/lib/views/browser/item.js#L528

Line 528: getColor: function () { //let vibrantjs do stuff in a promise }; uses vibrantjs and Q.defer and it does w/e it wants.

Line 130, 203, 508: We call that 'getColor' function from elsewhere like this: this.getColor(true).then(function (color) { //do stuff when ready }); so we're sure to have the data when it's ready and not before.


Q is available here: https://github.com/kriskowal/q

A tool for creating and composing asynchronous promises in JavaScript

jariz commented 9 years ago

Thanks for sharing. Yea, there's many ways to do this async, there's a lot of promises libraries. And to be honest, it's not that intensive of a calculation to begin with (takes a milisecond at max), so I don't really see the point of adding it to vibrant itself or something. Answer to the initial question: use Q, or ES6 promises
Offtopic: @sv244 told me you guys were using vibrant, I think it's really cool, popcorn time is my favorite way to waste time! If you have any questions, feel free to ask any time. (here or on twitter @jarizwarts)