Last active
July 13, 2017 18:12
-
-
Save muhammadfaizan/bdddc9ec970df2a146ad3f39a4efdf53 to your computer and use it in GitHub Desktop.
Looping through promises to fetch data.
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
let o = 0, l = 10; | |
let fetchAddress = (offset = 0, limit= 10) => { | |
let addresses = [...Array(100).keys()]; | |
return Promise.resolve(addresses.slice(offset, offset + limit)) | |
} | |
let task = (data) => { | |
// Process with data here; | |
let log = console.log.bind(console, "task: ") | |
log(data); | |
// increment iteration | |
o += l; | |
log((data.length == l)) | |
let p = fetchAddress(o,l); | |
log( "return", p); | |
return (data.length == l)? p : null; | |
} | |
let processWrapper = (task) => { | |
let results = []; | |
let process = p => { | |
let log = console.log.bind(console, "process: ") | |
log(p) | |
if (!p) return results; | |
return p.then(data => { | |
results = results.concat(data); | |
return process(task(data)); | |
}) | |
} | |
return process; | |
}; | |
let processSeries = processWrapper(task); | |
let log = console.log.bind(console, "Root: "); | |
processSeries(fetchAddress(o, l)).then(log).catch(console.error); |
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
let o = 0, l = 10; | |
let fetchAddress = (offset = 0, limit= 10) => { | |
let addresses = [...Array(100).keys()]; | |
return Promise.resolve(addresses.slice(offset, offset + limit)) | |
} | |
/*let results = []; | |
let process = p => { | |
if (!p) return p; | |
return p.then(data => { | |
// Process with data here; | |
console.log(data); | |
// increment iteration | |
o += l; | |
results = results.concat(data); | |
console.log((data.length == l)) | |
return (data.length == l)? process(fetchAddress(o, l)).then(data => data) : null; | |
}) | |
}*/ | |
//process(fetchAddress(o, l)).then(console.log).catch(console.error); | |
let task = (data, args) => { | |
let log = console.log.bind(console, "task: ") | |
// Process with data here; | |
// do not destructor args, like this, args should work as reference here. and should be handled accordingly. like in my condition, im using it to fetch data | |
let [o, l] = args; | |
let p = fetchAddress(o,l); | |
// Handling first call with true signal | |
if (data === true) return p | |
// increment iteration | |
args[0] += args[1]; | |
//log((data.length == l)) | |
//log( "return", p); | |
return (data.length == l)? p : null; | |
} | |
let processWrapper = (task, ...args) => { | |
let results = []; | |
let log = console.log.bind(console, 'ProcessWrapper: '); | |
log(args); | |
let params = [].concat(args); | |
let process = p => { | |
let log = console.log.bind(console, "process: ") | |
//log(p) | |
if (!p) return results.slice(1); | |
return p.then(data => { | |
if (data){ | |
results = results.concat(data); | |
} | |
return process(task.call(null, data, params)); | |
}) | |
} | |
return process; | |
}; | |
let processSeries = processWrapper(task, o, l); | |
let log = console.log.bind(console, "Root: "); | |
processSeries(Promise.resolve(true)).then(log).catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment