Using context.async on server side

Hi everyone,

i have a problem figuring out how to use the context.async function …

i’m using a library, server-side, whose main function is read(url, function (err, article, meta) {...});
i’m writing the following (but with no success) (in the run_server: function) :
function(properties, context) {
var read = require('node-readability');
return context.async(async callback => {
try {
var res = await read(properties.url, function(err, article, meta) {
var html = article.html;
var text = article.content;
article.close();
return {
html_body :html,
content: text
}
});
callback(undefined, res);
}
catch ( err ) {
callback( err );
}
});
}

And i can’t figure out what it doesn’t work …

Thanks for any help :slight_smile:

It should look like this:

let result = context.async(async callback => {
    try {
        var res = await read(...)
        callback(null, res);
    }
    catch (error) {
        callback(error);
    }
});

Then whatever you need to do with the result of the async call do it after.

What about arrayBuffers

from a quick view of node-readability readme.it looks like read is not an async function.
You should try something like

let result = context.async(cb => {
  read('your url',(err,article,meta)=>{
    cb(err,{article,meta})
  })
});

It’s probably a bad idea to use this package anyway because the last published version is from 5 years ago and it’s using outdated dependencies. For example it’s using jsdom version 9 but the current version is 20.
This can have security implications

in the end i don’t use it on server-side …
i decided to use the Readability.js on the front side : https://github.com/mozilla/readability

1 Like

much better :ok_hand:

2 Likes