Step.js expects callbacks for each step. Also, a function using Step should return a result and not return it.
So let's say you have:
function pullData(id, callback){ dataSource.retrieve(id, function(err, data){ if(err) callback(err); else callback(data); }); }
Using the step will work as follows:
var step = require('step'); function getDataFromTwoSources(callback){ var data1, data2; step( function pullData1(){ console.log('test1'); pullData(1, this); }, function pullData2(err, data){ if(err) throw err; data1 = data; console.log('test2'); pullData(2, this); }, function finalize(err, data) { if(err) callback(err); else { data2 = data; var finalList = [data1, data2]; console.log('done with no problem'); callback(null, finalList); } } ); };
This will make him follow the steps.
Please note that I personally prefer asynchronous operation for two reasons:
- The step catches all abandoned errors and calls back with them; this changes the behavior of the application when these libraries just need to clear the look of the code.
- Async has much better combination options and looks cleaner.
Oved d
source share