How to use the apply function from async

Find comprehensive JavaScript async.apply code examples handpicked from public code repositorys.

async.apply is a function in the async library for Node.js that allows a function to be partially applied with some of its arguments preset and returns a new function that can be called with the remaining arguments.

32
33
34
35
36
37
38
39
40
41
const utils = require('../../utils');
const request = utils.request;
const should = require('should');
const iotAgentLib = require('iotagent-node-lib');
const amqp = require('amqplib/callback_api');
const apply = async.apply;
let contextBrokerMock;
let oldTransport;
let amqpConn;
let channel;
fork icon82
star icon40
watch icon14

+ 2 other calls in file

501
502
503
504
505
506
507
508
509
510
    callback = options;
    options = {};
}

return Async.parallel({
    refreshToken: Async.apply(this._refreshTokenIfNeeded.bind(this), auth),
    convertAttachments: (callback) => {

        if (!params.attachments || params.attachments.length === 0) {
            return callback();
fork icon2
star icon1
watch icon6

How does async.apply work?

The async.apply function in the Async library in JavaScript creates a new function with the given arguments partially applied to the original function. When the new function is called, it will call the original function with the partially applied arguments followed by any additional arguments passed to the new function.

832
833
834
835
836
837
838
839
840
841
* @returns {Function} the partially-applied function
* @example
*
* // using apply
* async.parallel([
*     async.apply(fs.writeFile, 'testfile1', 'test1'),
*     async.apply(fs.writeFile, 'testfile2', 'test2')
* ]);
*
*
fork icon0
star icon2
watch icon1

+ 11 other calls in file

156
157
158
159
160
161
162
163
164
165
});

describe('listRemoveAll()', () => {
    before((done) => {
        async.series([
            async.apply(db.listAppend, 'testList5', 1),
            async.apply(db.listAppend, 'testList5', 1),
            async.apply(db.listAppend, 'testList5', 1),
            async.apply(db.listAppend, 'testList5', 2),
            async.apply(db.listAppend, 'testList5', 5),
fork icon0
star icon0
watch icon1

+ 14 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const async = require("async");

function add(a, b, callback) {
  callback(null, a + b);
}

const addFive = async.apply(add, 5);

addFive(10, (err, result) => {
  if (err) {
    console.error(err);
    return;
  }
  console.log(result);
});

// Output: 15

In this example, we define a function called add that takes two arguments a and b, and a callback function callback. Inside add, we call callback with the result of a + b. We then use async.apply to create a new function called addFive, which takes one argument (the second argument to add, in this case b), and returns a new function that takes one argument (the first argument to add, in this case a). Finally, we call addFive with 10 as its argument, which creates a new function that adds 5 to its argument. When we call that new function with 10, we get the result 15.

496
497
498
499
500
501
502
503
504
505
db.deleteObjectFields('testObject10', ['delete1', 'delete2'], function (err) {
    assert.ifError(err);
    assert(arguments.length < 2);
    async.parallel({
        delete1: async.apply(db.isObjectField, 'testObject10', 'delete1'),
        delete2: async.apply(db.isObjectField, 'testObject10', 'delete2'),
    }, (err, results) => {
        assert.ifError(err);
        assert.equal(results.delete1, false);
        assert.equal(results.delete2, false);
fork icon0
star icon0
watch icon1

+ 11 other calls in file

151
152
153
154
155
156
157
158
159
160
        next();
    })(req, res, next);
}, Auth.middleware.validateAuth, (req, res, next) => {
    async.waterfall([
        async.apply(req.login.bind(req), res.locals.user, { keepSessionInfo: true }),
        async.apply(controllers.authentication.onSuccessfulLogin, req, req.uid),
    ], (err) => {
        if (err) {
            return next(err);
        }
fork icon0
star icon0
watch icon1

+ 3 other calls in file

255
256
257
258
259
260
261
262
263
264
  return this.template.roles[title] || undefined;
};

Helper.prototype.getRolesAndForms = function(done) {
  async.series([
    async.apply(this.getForms.bind(this)),
    async.apply(this.getRoles.bind(this))
  ], (err) => {
    if (err) {
      return done(err);
fork icon0
star icon0
watch icon1

17
18
19
20
21
22
23
24
25
26
before((done) => {
    Groups.cache.reset();
    // Create 3 users: 1 admin, 2 regular
    async.series([
        async.apply(User.create, { username: 'foo', password: 'barbar' }), // admin
        async.apply(User.create, { username: 'baz', password: 'quuxquux' }), // restricted user
        async.apply(User.create, { username: 'herp', password: 'derpderp' }), // regular user
    ], (err, uids) => {
        if (err) {
            return done(err);
fork icon0
star icon0
watch icon1

+ 8 other calls in file

1191
1192
1193
1194
1195
1196
1197
1198
1199
1200

describe('sortedSetsRemove()', () => {
    before((done) => {
        async.parallel([
            async.apply(db.sortedSetAdd, 'sorted4', [1, 2], ['value1', 'value2']),
            async.apply(db.sortedSetAdd, 'sorted5', [1, 2], ['value1', 'value3']),
        ], done);
    });

    it('should remove element from multiple sorted sets', (done) => {
fork icon0
star icon0
watch icon1

+ 5 other calls in file