-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Resolve toBuffer Promise with an array (was: When using promises, how do you get image info) #143
Comments
As you've probably discovered, the single value passed to resolve a Promise is the Buffer containing image data only, which does not include the I believe the ES6 Promises spec will provide It's not entirely elegant, but with the code as it is right now you should be able to chain a call to resize with a call to metadata to achieve what you want using only Promises: sharp(input).resize(123).toBuffer()
.then(function(data) {
return [data, sharp(data).metadata()];
})
.spread(function(data, info) {
// now have data and info
}); |
Is it possible to maybe pass a boolean to |
I'm not a big fan of boolean parameters as they encourage the use of inline A named parameter in the form of an Object would be better, but given array-returning Promises are soon to be a "standard" thing, I think I'd prefer to break the API (as part of a major version revision) to always return an array. If it's OK with you I'd like to change this issue into a feature request to have the Promise resolve with a Were you able to try the workaround with the chained called to |
I am using a workaround so things are currently ok. please edit and/or close this issue as you see fit. Thanks |
Cheers Samy, thank you for reporting this inconsistency in the API in the first place, much appreciated. |
Workaround using the Bluebird promise library: var sharp = require('sharp'),
Promise = require('bluebird');
Promise.promisifyAll(sharp.prototype, {multiArgs: true});
// promisify the sharp prototype (methods) to promisify the alternative (for raw) callback-accepting toBuffer(...) method
// for demonstration purposes
sharp('./test.png')
.raw().toBufferAsync() // notice the [...]Async method name, generated by bluebird
.then(function(dataAndInfo) {
// `multiArgs: true` further above will spread the arguments passed to the callback function to array elements by their order
var data = dataAndInfo[0], // first the data, then the info
info = dataAndInfo[1];
// nicely assigning the array elements back to variables for demonstration purposes
return sharp(data, {raw: info}) // as usual...
.metadata(); // triggering sharp to actually load that data (for demonstration purposes that this works)
})
.then(function(meta) {
console.log(meta); // should succesfully log the determined metadata
}); Hope this helps! |
Given there's no easy deprecation route how about we go with a slight modification of @salzhrani's original suggestion of allowing This would look something like |
From my point of view, it would be better if there would be a completely new function called |
@Vispercept The Rather than the Promise resolving with an Array, how about resolving with an Object, which would allow for destructuring e.g.: sharp(input)
.resize(123)
.toBuffer({ resolveWithObject: true })
.then( ({ data, info }) => {
...
}); |
@lovell thanks for the clarification :-) didn't know that... |
Commit 6fe5b30 adds the new |
v0.17.3 now available, thanks everyone for the suggestions and discussions. |
if a callback is used with
toBuffer
for example the imageinfo
is passed to the callback, how do you get this info when using a promise ?The text was updated successfully, but these errors were encountered: