Created
February 26, 2017 00:50
-
-
Save kenmori/c65be0fedc238965645854c20cbf6af4 to your computer and use it in GitHub Desktop.
co
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
import React, { Component } from 'react' | |
import ReactDOM from 'react-dom' | |
import 'babel-polyfill'; | |
import co from 'co' | |
class Greet extends Component { | |
render() { | |
co(function *(){ | |
// yield any promise | |
var result = yield Promise.resolve(true); | |
console.log(result); | |
}).catch(onerror); | |
var fn = co.wrap(function* (fa){ | |
return yield Promise.resolve(fa); | |
}); | |
fn(true).then(function(val){console.log(val)}) | |
const promiseFun = co.wrap( function* (url){ | |
return new Promise(function(resolve, reject){ | |
var req = new XMLHttpRequest(); | |
req.open('GET', url, true); | |
req.onload = function(){ | |
if(req.status == 200){ | |
setTimeout(()=>{ | |
//5秒後にdone | |
resolve(req.response); | |
}, 5000) | |
} else { | |
reject(Error("error")); | |
} | |
} | |
req.onerror = function(){ | |
console.log("error"); | |
} | |
req.send(null); | |
}); | |
}) | |
//並列処理 | |
// promiseFun('http://localhost:3000/comments').then((res)=> console.log(res)) | |
co(function* (){ | |
var res = yield [ | |
promiseFun('http://localhost:3000/posts'), | |
promiseFun('http://localhost:3000/profile'), | |
promiseFun('http://localhost:3000/state'), | |
]; | |
console.log(res[0], res[1], res[2]); | |
}).catch(onerror); | |
function onSuccess(result) { | |
console.log('success', result) | |
} | |
function onError(e) { | |
console.log('error', e) | |
} | |
let p = co.wrap(function *(i){ | |
return yield new Promise((resolve, reject) => { | |
return i%2 === 0 ? reject(new Error(i)) :resolve(i) | |
}); | |
}); | |
p(1).then(onSuccess).catch(onError) | |
return ( | |
<ul id='result'></ul> | |
) | |
} | |
} | |
window.addEventListener('DOMContentLoaded', ()=>{ | |
ReactDOM.render(<Greet />, document.querySelector('main')); | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment