-1

still learning to javascript. How does this below code work? when main(params) is called does it mean it under-the-hood calls dowork(callbackXYZ, options, params) ?

const { dowork } = require('some_lib');

async function callbackXYZ(src, tgt, params) => {
    // ...logic
}
const main = dowork(callbackXYZ, options);
await main(params);
2
  • dowork probably returns a function, which you call with params. Commented Jul 16, 2020 at 22:44
  • 2
    There's no way to tell from the snippet what happens inside the function that doworks returns. It depends on what's in some_lib. Commented Jul 16, 2020 at 22:48

2 Answers 2

0

Here's a simplified code example. It removes the async stuff, since it's not needed to understand.

// The callback you provide to `dowork`
// The internals of `dowork` will give invoke it with the given args
function callbackXYZ(src, tgt, params) {
   console.log("src is:", src);
   console.log("tgt is:", tgt);
   console.log("params is:", params);
}

// The options you provide to `dowork`
var options = {src: "foo", tgt: "bar"};

// Invoking `dowork` receives your callback and options, and returns
// a new function that has access to both of these arguments.
const main = dowork(callbackXYZ, options);

// The params you provide to the returned function
const params = {my: "params"};

// Invoking the returned function
main(params);


// Here's the `dowork`. It receives your callback and options, and it
// returns a new function that expects you to pass it params.
// So that returned function has reference to the callback, options
// and params.
// The body of the returned function, simply invokes your callback and
// passes it data from the options and params you provided
function dowork(callback, opts) {
  return function(params) {
    callback(opts.src, opts.tgt, params);
  }
}

So dowork receives your callbackXYZ and the opts and returns a function that you can invoke, passing in the params.

When you invoke that returned function, and pass it the params, that returned function invokes your original callback, and passes it data both from the options and the params.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @slappy for the detailed explanation. Got it. Is this a common javascript pattern? You immediately(correctly) recognized that dowork(...) was returning function.
@gkucmierz @slappy to me it looks like a way to provide flexibility. i.e the caller can write their own callbackXYZ and dowork() can invoke it. That is obvious. But dowork() returning a function and and then caller calling that function seems like additional hops.
Yes, passing functions around (including returning them) is very common. Because functions will "remember" the original variable scope where they're created (they create a closure), they're handy for associating some data with a function that continuously operates with that data.
0

No!

  • dowork is function which returns another function
  • that another function is assigned to const main
  • then this main (function which returns promise) is called

You can also run this code without declaring anything:

await dowork(callbackXYZ, options)(params);

1 Comment

And what dowork() does with all these parameters depends on the design of the library.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.