Last active
February 11, 2021 14:47
-
-
Save rajivnarayana/369b034d5bd395c3911f6d262d98310c to your computer and use it in GitHub Desktop.
nodejs async waterfall error workaround
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This is a modified form of issue https://github.com/caolan/async/issues/1611 with title | |
* "waterfall Callback was already called. #1611" | |
* | |
* Reason: | |
* First, the line no 16 executes causing the next waterfall step to execute. | |
* Subsequently line no 25 causes an exception which is gaurded by Promise.then in the first step | |
* which in turn causes catch block in line no 21 to be executed. | |
*/ | |
const async = require("async"); | |
async.waterfall([ | |
(cb) => { | |
return new Promise((resolve) => { | |
setTimeout(() => { | |
resolve(); | |
}, 1000); | |
}).then(() => { | |
return cb(null); | |
}).catch((err) => { | |
return cb(err); | |
}); | |
}, | |
(cb) => { | |
const a = undefined; | |
const b = a.dummyData; | |
return cb(null, b); | |
}, | |
], (err) => { | |
if (err) return console.error(err); | |
return console.log("Done with all functions in the waterfall waterfall"); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const async = require("async"); | |
const once = function(callback) { | |
return function() { | |
if (callback == null) { | |
// console.trace("Callback was called multiple times"); | |
return; | |
} | |
var callFn = callback; | |
callback = null; | |
callFn.apply(this, arguments); | |
} | |
} | |
/** | |
* Added the above function to prevent callback from being executed multiple times.. | |
* Modified cb to `origCb` in line no 18 using cb = once(origCb) | |
*/ | |
async.waterfall([ | |
(origCb) => { | |
const cb = once(origCb); | |
return new Promise((resolve) => { | |
setTimeout(() => { | |
resolve(); | |
}, 1000); | |
}).then(() => { | |
return cb(null); | |
}).catch((err) => { | |
return cb(err); | |
}); | |
}, | |
(cb) => { | |
const a = undefined; | |
const b = a.dummyData; | |
return cb(null, b); | |
}, | |
], (err) => { | |
if (err) return console.error(err); | |
return console.log("Done with all functions in the waterfall waterfall"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment