What is the 'this' Binding in Async 'seq' and 'compose' Functions?
TLDR: global
Async is a powerful utility module for working with asynchronous programming style in JavaScript. In the documentation, there are a couple places talking about this:
Each function is executed with the this binding of the composed function. - async
This affects these two functions:
seq(fn1, fn2...)
compose(fn1, fn2...)
When printing the this context from a composed function, this is what we have:
1
2
3
4
5
6
7
8
9
10
11
12
[ 'global',
'process',
'GLOBAL',
'root',
'Buffer',
'setTimeout',
'setInterval',
'clearTimeout',
'clearInterval',
'setImmediate',
'clearImmediate',
'console' ]
This looks very much like the global namespace object.
Let’s verify it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Async: This Binding
// ===================
//
// Find out the `this` binding in Async composed functions.
'use strict';
// Dependencies
// ------------
var async = require('async');
// Functions
// ---------
functionfn1(done){
console.log('fn1: this === global ?', this === global);
setTimeout(function(){
done();
}, 10);
}
functionfn2(done){
console.log('fn2: this === global ?', this === global);