Last change
on this file since 6c1585f was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
599 bytes
|
Line | |
---|
1 | var defer = require('./defer.js');
|
---|
2 |
|
---|
3 | // API
|
---|
4 | module.exports = async;
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * Runs provided callback asynchronously
|
---|
8 | * even if callback itself is not
|
---|
9 | *
|
---|
10 | * @param {function} callback - callback to invoke
|
---|
11 | * @returns {function} - augmented callback
|
---|
12 | */
|
---|
13 | function async(callback)
|
---|
14 | {
|
---|
15 | var isAsync = false;
|
---|
16 |
|
---|
17 | // check if async happened
|
---|
18 | defer(function() { isAsync = true; });
|
---|
19 |
|
---|
20 | return function async_callback(err, result)
|
---|
21 | {
|
---|
22 | if (isAsync)
|
---|
23 | {
|
---|
24 | callback(err, result);
|
---|
25 | }
|
---|
26 | else
|
---|
27 | {
|
---|
28 | defer(function nextTick_callback()
|
---|
29 | {
|
---|
30 | callback(err, result);
|
---|
31 | });
|
---|
32 | }
|
---|
33 | };
|
---|
34 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.