When can I use lodash.after ()?

I am new to functional programming, and someone recommended lodashto understand the basic functions of a higher order.

Looking at the lodash API docs, I could not understand the sample code for _.after():

var saves = ['profile', 'settings'];

var done = _.after(saves.length, function() {
  console.log('Done saving!');
});

_.forEach(saves, function(type) {
  asyncSave({ 'type': type, 'complete': done });
});
// → logs 'Done saving!', after all saves have completed

The sample code above did not implement asyncSave(), so I have to implement this function for myself. But I do not know how to implement it. This is because I do not understand the specifications myself _.after().

It will be great if someone explains this function more easily than the API docs say. Or a simple, clear, and practical example of a function would be a big help. Thanks!

+4
source share
1

_.after count n f, , fp ( "f prime" ). fp , , . fp n, fp ( , , ). fp , n, f.

:

var _ = require("lodash");
var fp = _.after(3, function () { console.log("OMG!"); });
fp(); // Nothing
fp(); // Nothing
fp(); // Prints "OMG!"

asyncSave , ( type ), (complete , ) .

, _forEach asyncSave saves, , saves. done asyncSave, done . done _.after, , _.after, saves.length, , _.after, , done saves.length . , , , , .

+7

Source: https://habr.com/ru/post/1523693/


All Articles