Created
May 16, 2019 05:56
-
-
Save akari0624/65e38ef29c44d8bb7758c3359ca993fe to your computer and use it in GitHub Desktop.
generator
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 pFetch = fetch | |
if(!fetch){ | |
pFetch = require('node-fetch') | |
} | |
const url = 'http://numbersapi.com' | |
//const url = 'https://api.tvmaze.com/search/shows?q=batman' | |
function* getNumberStr(){ | |
console.log('in ggg') | |
yield pFetch(`${url}/1`, {method: 'get'}) | |
yield pFetch(`${url}/2`, {method: 'get'}) | |
yield pFetch(`${url}/3`, {method: 'get'}) | |
yield pFetch(`${url}/4`, {method: 'get'}) | |
yield pFetch(`${url}/5`, {method: 'get'}) | |
yield pFetch(`${url}/6`, {method: 'get'}) | |
console.log('ggg end') | |
} | |
const getResult = (ite) =>{ | |
let n = 0 | |
for(let curr of ite){ | |
curr.then( res => { | |
n+=1 | |
if(n>3){ | |
ite.throw(new Error('take enough~')) | |
} | |
return res.text() | |
}).then(text => { | |
console.log(text) | |
}).catch(err => { | |
console.error(err) | |
}) | |
} | |
} | |
const ite = getNumberStr() | |
getResult(ite) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment