xavi- / node-copy-paste

A command line utility that allows read/write (i.e copy/paste) access to the system clipboard.
425 stars 76 forks source link

Callback for paste() doesn't returns "null" #34

Closed frdmn closed 9 years ago

frdmn commented 9 years ago

When I try to use paste() with it's callback function, it always returns with null. Here's an example:

var clipboard = require('copy-paste');

function getClipboard(cb){
  clipboard.paste(function(data){
    return cb(data);
  });
}

getClipboard(function(data){
  console.log(data);  
})
// => null

Without the callback it works just fine:

var clipboard = require('copy-paste');

function getClipboard(){
  return clipboard.paste();
}

console.log(getClipboard());
// => [CONTEN_OF_CLIPBOARD]
HerrZatacke commented 9 years ago

Hi @frdmn the callback function has two parameters. The first should contain the Error if one ocurred. The second parameter ist the Clipboard-data.

this should work:

var clipboard = require('copy-paste');

function getClipboard(cb){
  clipboard.paste(function(err, data){
    if(err) { return cb(false); }
    return cb(data);
  });
}

getClipboard(function(data){
  console.log(data);  
})
xavi- commented 9 years ago

Thanks @HerrZatacke. @frdmn is 100% correct. Hope that helps.

frdmn commented 9 years ago

@HerrZatacke @xavi- Ah this makes sense, thanks a lot!