-1

I'm using the package WordPos which has a function getNouns such that getNouns(text, callback), for example.

wordpos.getNouns('The angry bear chased the frightened little squirrel.', 
console.log)
// [ 'bear', 'squirrel', 'little', 'chased' ]

I want to write the promise to an array rather than logging it and haven't had any luck. Any suggestions?

tittimous
  • 39
  • 4

2 Answers2

1

Why don't you just do

wordpos.getNouns('The angry bear chased the frightened little squirrel.', result => {
  const array = result;
  // do stuff with `array`
})
Aron
  • 8,696
  • 6
  • 33
  • 59
1

You have to wrap this function with a Promise.

function getNounsPromise(string) {
  return new Promise((resolve, reject) => {
    wordpos.getNouns(string, resolve);
  });
} 

// usage:
getNounsPromise('The angry bear chased the frightened little squirrel.')
  .then(result => console.log(result)); // [ 'bear', 'squirrel', 'little', 'chased' ]
Michał Pietraszko
  • 5,666
  • 3
  • 21
  • 27