0

I have the following code:

function done(err, file) {

  //handle code

  return "test";
}

function process() {

    retext()
     .use(keywords)
     .process(sentence, done);
  

    return val; 
}

The process() function must return the value of retext(), but the retext() function doesn't seem to return anything.

If I change it to:

const val = retext()
     .use(keywords)
     .process(sentence, done);

That still doesn't work

How can I get retext() to return something. It is just an NPM package I installed

I tried returning test in the done() function but that doesn't pass anything to return to retext().

I have also tried:

retext()
      .use(keywords)
      .process(params.t, done).then((val) => return val);

To summarise:

process() must return the value of done(), which is called by retext()

ewfewfjio
  • 37
  • 6
  • `retext` _does_ return something, otherwise this chaining would be impossible. But `process` probably doesn't return anything. – tkausl Jul 24 '22 at 21:33
  • I want to get the value of `retext()`, how do I do that? – ewfewfjio Jul 24 '22 at 21:33
  • 1
    `const result = retext(); result.use(keywords).process(sentence, done); return result;` But I _really_ doubt that's what you meant. – tkausl Jul 24 '22 at 21:35
  • Have you tried `console.log`ging what this actually returns? – tkausl Jul 24 '22 at 21:35
  • @tkausl That seems close to what I meant, in your example, I just need `return result` to return something. The `done()` function returns a string and works correctly, but I just need to store that value from that function in `result` called from `retext()` – ewfewfjio Jul 24 '22 at 21:39
  • Oh, _now_ I got it. Alright, so what you're trying to do is not possible. Your function will return _before_ done is even called. – tkausl Jul 24 '22 at 21:40
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – tkausl Jul 24 '22 at 21:40
  • @tkausl `return result` in your example just returns the `retext()` function itself, and ignores the `.process(sentence, done);` value – ewfewfjio Jul 24 '22 at 21:40
  • @tkausl Nope unfortunately, – ewfewfjio Jul 24 '22 at 21:42

1 Answers1

0

I am not familiar with the retext package. However from the documentations available here, the retext function looks like it's asynchronous. As such you will need to await the result. You will also need to convert the process into an async function. However, without a reproducible/working problem, it's difficult to suggest solutions.

If I am missing something, please share a fiddle or some code snippet we can replicate.

async function process() {
  const val = await retext()
    .use(keywords)
    .process(sentence, done);
  console.log(val); // should work now
  return val;
}

Additionally when you are calling the process function, you'll need to await again.

Here is a very nice tutorial on async/await in javascript.

dotdeb
  • 126
  • 1
  • 8
  • This would only work if `process` returns a Promise. Given that OP passes a callback to it I guess it doesn't. – tkausl Jul 24 '22 at 21:57
  • Yeah, probably not. I did not see the comments under the original post when I posted. I presumed it was an async await issue when I saw `retext` was async – dotdeb Jul 24 '22 at 21:59
  • @tkausl This didn't work but I found a workaround where I just use global variables, set the global variables in `done()` – ewfewfjio Jul 24 '22 at 22:08
  • @ewfewfjio just for posterity, what are `keywords` and `sentence` are they a list of strings? Any way we can recreate the issue? Just in case someone else stumbles across this thread. – dotdeb Jul 24 '22 at 22:12
  • @dotdeb `keywords` is just an NPM package I got from doing `const keywords = require('retext-keywords');` `Sentence` is just any string, it will take it and extract the keywords – ewfewfjio Jul 24 '22 at 22:22